2

I hope someone might be able to help me. I am trying to parse following json file:

{"seminar":[
    {"categoryid": "1","cpe": "13","inventory":["Discussion","Value x","Value y"
    ],"teachers": [
    {
        "titel": "Dipl.-Ing.",
        "company": "XY",
        "name": "Test",
        "id": "3",
    }
    ],

},...

I am lost with parsing the teachers data in...

...
private static final String TAG_teachers = "teachers";
private static final String TAG_TITLE = "title";

for(int i = 0; i < seminar.length(); i++){
    JSONObject c = seminar.getJSONObject(i);
    ...
    teachers = c.getJSONArray(TAG_DOZENTEN);
    for(int z = 0; z < teachers.length(); z++){                 
    JSONObject d = teachers.getJSONObject(z);
    String title  = d.getString(TAG_TITLE);
    Log.d("JSONParsingActivity", title);

I get the error System.err(1010): org.json.JSONException: Value null at teachers of type org.json.JSONObject$1 cannot be converted to JSONArray.

What did I do wrong? As I understand from the JSON documentation, teachers is an JSON Array and not an Object. Is somebody able to help me?

1
  • 1
    The answers appear correct, but I would also suggest jsonlint.com Commented Nov 26, 2012 at 22:16

2 Answers 2

3

You have an extra (trailing) comma in teachers (after "3"). Not allowed in JSON. Remove it and see if that helps.

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

Comments

2

If your JSON is really of the form:

{ ... }, { ... }, { ... }, ...

This is invalid JSON

The root enclosure must either be a single object (in {}) or an array (in []).

If your intent is to send an array of objects, then simply wrap the entire thing with square brackets to make it an array and create a JSONArray object from it.

So it must be like this

[ { ... }, { ... }, { ... }, ... ]

You also need to make sure that you don;t have extra commas, unclosed brackets, etc. Use JSONLint or other similar JSON format checker to save yourself some time in finding syntax problems.

2 Comments

Thanks a lot for your help! My JSON code is valid. These errors just happened, while I tried to shorten my code for this platform.
Opened up a new question, as I put invalid json here

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.