0

I have the following json returned by web api but I'm getting the following error when trying to parse it: this is not a json array.

{
"$id": "1",
"updateTime": "2013-12-10T12:28:26.6533786+00:00",
"user": {
    "$id": "2",
    "CompanyID": 1,
    "username": "test",
    "Firstname": "test",
    "Lastname": "ing"
},
"Vehicles": {
    "$id": "3",
    "$values": [
        {
            "$id": "4",
            "VehicleID": 1,
            "CompanyID": 1,
            "VehicleReg": "123",
            "VehicleTypeID": 1,
            "VehicleDescription": ""
        },
        {
            "$id": "5",
            "VehicleID": 4,
            "CompanyID": 1,
            "VehicleReg": "456",
            "VehicleTypeID": 2,
            "VehicleDescription": ""
        },
        {
            "$id": "6",
            "VehicleID": 7,
            "CompanyID": 1,
            "VehicleReg": "789",
            "VehicleTypeID": 3,
            "VehicleDescription": ""
        }
    ]
},
"VehicleTypes": {
    "$id": "7",
    "$values": [
        {
            "$id": "8",
            "VehicleTypeID": 1,
            "VehicleType": "First Test Type",
            "CompanyID": 1
        },
        {
            "$id": "9",
            "VehicleTypeID": 2,
            "VehicleType": "Second Test Type",
            "CompanyID": 1
        },
        {
            "$id": "10",
            "VehicleTypeID": 3,
            "VehicleType": "Third Test Type",
            "CompanyID": 1
        }
    ]
},
"Questions": {
    "$id": "11",
    "$values": [
        {
            "$id": "12",
            "QuestionID": 1,
            "SectionID": 1,
            "CompanyID": 1,
            "Question": "Question 1"
        },
        {
            "$id": "13",
            "QuestionID": 2,
            "SectionID": 1,
            "CompanyID": 1,
            "Question": "Question 2"
        },
        {
            "$id": "14",
            "QuestionID": 3,
            "SectionID": 1,
            "CompanyID": 1,
            "Question": "Question 3"
        }
    ]
},
"QuestionSections": {
    "$id": "15",
    "$values": [
        {
            "$id": "16",
            "SectionID": 1,
            "CompanyID": 1,
            "SectionName": "Section 1"
        }
    ]
}

}

I have put this through a parser and it does say it's valid json.

I'm trying to parse the Vehicles in the json like so:

Gson gsonv = new Gson();
JsonParser parser = new JsonParser();               
JsonArray Jarray = (JsonArray)parser.parse(reader).getAsJsonArray();
ArrayList<VehicleEntity> lcs = new ArrayList<VehicleEntity>();
for(JsonElement obj : Jarray )
{
VehicleEntity v = gsonv.fromJson( obj , VehicleEntity.class);
lcs.add(v);
Log.d(TAG, "Vehicles: " + v.VehicleID);
}

My VehicleEntity matches the properties in the json Vehicles portion.

Am I on the right path here or is it a problem with the json returned by the web api?

Thanks Paul

1
  • I never used gson so may be i'm wrong, but the first nod you have is not a json array, in fact none of the root elements are jsonarrays Commented Dec 13, 2013 at 15:29

1 Answer 1

1

The top-level JSON is a JSONObject (surrounded by {}, with keys) not a JSONArray (surrounded by [], basically a list).

It sounds like you're expecting an array of these Objects, but it doesn't look like that's what you're getting.

If you wrap that object in '[' and ']' you'd have an array of length 1, and maybe your code would work.

There are sometimes server settings that send a single Object instead of a JSONArray if there is only 1 element. Maybe that's the case? What if you preform a server query that returns multiple results? Then does your code work?

Sign up to request clarification or add additional context in comments.

5 Comments

looking at the json there are 3 vehicles I just wasn't expecting $ids $ values: tags and maybe this is the problem? Can I point the parser to give me back Vehicles/values? because after that is wher the objects are surrounded with []. Not at computer to test at moment but do you think this would work? Thanks
Yes, "$values" is a valid JSONArray... If you're asking how to get the vehicles array from the top-level JSONObject, it's probably something like this (untested): Gson gsonv = new Gson(); JsonParser parser = new JsonParser(); JsonObject topJsonObject = (JsonArray)parser.parse(reader).getAsJsonObject(); JsonArray jsonArray = topJsonObject.getJsonObject("Vehicles").getJsonArray("$values");
yeah tried that but compiler doesn't like: JsonObject topJsonObject = (JsonArray)parser.parse(reader).getAsJsonObject(); Cannot convert from JsonArray to JsonObject.
If I run the following code JsonObject userObj = (JsonObject)parser.parse(reader).getAsJsonObject().get("user"); it will give me back a user object yet if I run JsonObject topJsonObject = (JsonObject)parser.parse(reader).getAsJsonObject().get("Vehicles"); I receive an error Not a JSON Object: null. Any thoughts?
got it working it was a stupid error on my part, basically I was reading the input stream as follows JsonObject userObj = (JsonObject)parser.parse(reader).getAsJsonObject().get("user"); but when I tried to get the 'Vehicles' like so: JsonObject vehObj = (JsonObject)parser.parse(reader).getAsJsonObject().get("Vehicles"); I was getting endOfInput = true which was causing 'Not a JSON Object' error. If I read it like so JsonObject topObj = (JsonObject)parser.parse(reader).getAsJsonObject(); I can then use topObj to get the vehicles. Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.