JSON转换导致java失败(JSONObject无法转换为JSONArray)

时间:2011-07-29 13:30:30

标签: java android json

我的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);
}

这不是正确的提取方法吗?

3 个答案:

答案 0 :(得分:7)

您在对象和数组之间感到困惑。

您发布的代码示例都是对象{},而数组是[]

您拥有的结构是:{[{}]}

而您的解析代码段需要:[{}]

错误中有一个很大的暗示:     类型org.json.JSONObject无法转换为JSONArray

对象是键:值对的无序列表。

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

数组是有序(索引)值列表:

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

答案 1 :(得分:2)

克里斯和查尔斯的答案是正确的,你应该查看JSON specification。也就是说,我相信您正在寻找以下内容。

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

答案 2 :(得分: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"
                }
            }
        }
    }
}

这是JSON的外观,

正如Charles所说的Object {} Arrays [],这是一个学习JSON的方便备忘单

<强>对象 {} {members} 的成员信息 对 对,成员 的 字符串值 的阵列 [] [要素] 的元素 值 价值,要素 的 串 数 宾语 排列 真正 假 空