簡體   English   中英

Java中的JSON轉換結果失敗(無法將JSONObject轉換為JSONArray)

[英]JSON converting result in java failed (JSONObject cannot be converted to JSONArray)

我的PHP腳本的JSON結果有問題。 我無法在JAVA中提取此結果。 出現以下錯誤:

Error converting result org.json.JSONException: Value 
{"3":[{"date":"25\/07\/2011","descr":"Une colloque bla bla","numColloque":"1","titre":"Une colloque"}],
 "2":[{"mail":"jm@dvaid.fr","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

我的結果的語法很簡單:

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

我用於提取的JAVA腳本(ligne錯誤:帶有**):

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

這不是提取的正確方法嗎?

您對對象和數組感到困惑。

您發布的兩個代碼示例都是對象{}而數組是[]

您的結構是: {[{}]}

而您的解析代碼段應為: [{}]

錯誤中有一個大提示:類型org.json.JSONObject無法轉換為JSONArray

對象是鍵:值對的無序列表。

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

數組是值的有序(索引)列表:

[ // array
    "value1",
    "value2"
]

Chris和Charles的答案是正確的,您應該查看JSON規范 就是說,我相信以下是您正在尋找的東西。

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);
}
{
    "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"
                }
            }
        }
    }
}

這就是JSON的外觀,

正如Charles所說的Object {} Arrays [],這是學習JSON的便捷備忘單

對象 {} {個成員} 成員對對,成員字符串:值數組 [] [元素] 元素值value,元素字符串數對象數組true false null

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM