1

I have a problem with the JSON result of my PHP script. I can't extract this result in JAVA. There is the error below :

Error converting result org.json.JSONException: Value 
{"3":[{"date":"25\/07\/2011","descr":"Une colloque bla bla","numColloque":"1","titre":"Une colloque"}],
 "2":[{"mail":"[email protected]","descr":"truc truc","nom":"Une personne","tel":"0600000000","numPersonne":"1"}],
 "1":[{"lien":"http:\/\/www.irdes.fr","numTypeActu":"1","date":"25\/07\/2011","titre":"Une actualité récente","numActu":"1"}],
 "7":[{"numEtablissement":"1","specialite":"STG","mention":"une mention","modalite":"BTS","titre":"Un titre de formation","numFormationCours":"1"}],
 "6":[{"numEtablissement":"1","numEnseignement":"1","titre":"Cours de bla bla","numEnseignant":"1"}],
 "5":[{"date":"31\/07\/2011","descr":"Université paris descartes dans le 1-ème arrondissement de Paris","numEtablissement":"1","libelle":"IUT Paris DESCARTES"}],
 "4":[{"numDocument":"1","lienPDF":"http:\/\/www.irdes.com","date":"25\/07\/2011","numTypeDocument":"1","descrRapide":"un glossaire qui regroupe du bla bla","nom":"un document de glossaire"},
      {"numDocument":"2","lienPDF":"http:\/\/www.irdes.com","date":"25\/07\/2011","numTypeDocument":"2","descrRapide":"Une synthèse parlant d'un truc","nom":"Une synthèse"}],
 "9":[{"lien":"http:\/\/www.irdes.fr","descr":"un séminaire sur le blabla","date":"25\/07\/2011","heure":"14h30","nom":"Un séminaire","numSeminaire":"1"}],
 "8":[{"numPublication":"1","lienPDF":"http:\/\/www.irdes.com","date":"25\/07\/2011","numTypePublication":"1","titre":"Une publication"},
      {"numPublication":"2","lienPDF":"http:\/\/www.irdes.com","date":"25\/07\/2011","numTypePublication":"2","titre":"un titre de publication"},
      {"numPublication":"3","lienPDF":"http:\/\/www.irdes.com","date":"25\/07\/2011","numTypePublication":"3","titre":"un titre"}]} 

of type org.json.JSONObject cannot be converted to JSONArray

The syntax of my result is simple :

{"Object1":[{"id":"value"},{"id":"value"}],
 "Object2":[{"id":"value"},{"id":"value"}],
 ...
}

My JAVA script for the extraction (ligne error : with an **):

result=sb.toString();
**JSONArray jArray = new JSONArray(result);**
for (int i=1;i<=9;i++){
    JSONObject typeUpdateObject = jArray.getJSONObject(i);
    extraction(typeUpdateObject, i);
}

This is not the right method for this extraction ?

3 Answers 3

7

You are getting confused between objects and arrays.

Both the code samples you posted are objects {} whereas arrays are [].

The structure you have is: {[{}]}

Whereas your parsing code snippet is expecting: [{}]

There is a big hint in the error: type org.json.JSONObject cannot be converted to JSONArray

An object is an unordered list of key:value pairs.

{ // object
    "key" : "value",
    "key2" : "another value"
}

An array is an ordered (indexed) list of values:

[ // array
    "value1",
    "value2"
]
Sign up to request clarification or add additional context in comments.

1 Comment

I understood that, but my PHP code encode an array, an array of Objects values. Thanks for your explain.
2

The answers by Chris and Charles are correct and you should look over the JSON specification. That said, I believe the following is what you're looking for.

JSONObject obj = new JSONObject(result);
for(int i=1;i<=9;i++) {
    JSONArray arr = obj.getJSONArray(""+i);
    for(int j=0;j<arr.length();j++)
        extraction(arr.getJSONObject(j), i);
}

1 Comment

Thanks for your code Matt :) just for the second for, it's j variable :) (j<arr.length())
1
{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": ["GML", "XML"]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}

This is how a JSON should look,

as Charles said Object {} Arrays [], this is a handy cheat sheet for learning JSON

object {} { members } members pair pair , members pair string : value array [] [ elements ] elements value value , elements value string number object array true false null

Comments

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.