您好我正在尝试从我的网站解析 json。我从 te httpresponse 获得的 jsonString 工作正常,但是当我尝试在 JSONObject 中解析它时,它会给出 NPE。这是现在正在测试的 json:
{ "galgjejson" :
{ "Nederlands" :
{ "length7" : [
{ "word" : "android" },
{ "word" : "camping" },
{ "word" : "koekjes" }
]
},
{ "length8" : [
{ "word" : "androids" },
{ "word" : "campings" },
{ "word" : "campings" },
{ "word" : "campings" },
{ "word" : "campings" },
{ "word" : "campings" },
{ "word" : "scheppen" }
]
},
{ "length9" : [
{ "word" : "verslapen" },
{ "word" : "versleten" },
{ "word" : "verbrande" }
]
},
{ "length10" : [
{ "word" : "verslapend" },
{ "word" : "versletend" },
{ "word" : "verbranden" },
{ "word" : "verbranden" },
{ "word" : "verbranden" }
]
},
{ "length11" : [
{ "word" : "verslapende" },
{ "word" : "versletende" }
]
},
{ "length12" : [
{ "word" : "verslapenden" }
]
}
},
{ "English" :
{ "length7" : [
{ "word" : "leavers" },
{ "word" : "shoping" },
{ "word" : "cooking" }
]
},
{ "length8" : [
{ "word" : "stopping" },
{ "word" : "sleeping" },
{ "word" : "starting" },
{ "word" : "starting" },
{ "word" : "starting" },
{ "word" : "starting" },
{ "word" : "starting" }
]
},
{ "length9" : [
{ "word" : "somewordd" },
{ "word" : "somewordd" },
{ "word" : "somewordd" }
]
},
{ "length10" : [
{ "word" : "someworddd" },
{ "word" : "someworddd" }
]
},
{ "length11" : [
{ "word" : "somewordddd" },
{ "word" : "somewordddd" },
{ "word" : "somewordddd" },
{ "word" : "somewordddd" },
{ "word" : "somewordddd" }
]
},
{ "length12" : [
{ "word" : "someworddddd" },
{ "word" : "someworddddd" },
{ "word" : "someworddddd" }
]
}
}
}
我做的方法:
public MikeyJSON(String jsonString) {
Log.i("JSON", "jsonString: " + jsonString);
try {
JSONObject myObject = new JSONObject(jsonString);
Log.i("JSON", "myObject_Object: " + myObject.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getResult(String lang, String length, int wordPos) {
String word = "0";
try {
langObject = myObject.getJSONObject(lang);
Log.i("JSON", "Language_Object " + langObject.toString());
lengthObject = langObject.getJSONObject(length);
Log.i("JSON", "wordlength_Object " + lengthObject.toString());
wordArray = lengthObject.getJSONArray(length);
Log.i("JSON", "wordlength_Object " + wordArray.toString());
if(wordPos>wordArray.length()) {
wordPos = 0;
}
for(i=0;i<wordArray.length();i++){
word = wordArray.getJSONObject(wordPos).toString();
}
Log.i("JSON", "word_Object " + word);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return word;
}
}
我要检索的只是给定语言和长度的一个单词。这是我经过大量研究后所做的,但我只是不明白为什么它不起作用。如果我的 json 无效,请告诉我我做错了什么,因为我在 NPE 之前收到此错误:
03-18 09:17:40.052: W/System.err(1464): org.json.JSONException: Names must be strings,
but {"length8":[{"word":"androids"},{"word":"campings"},{"word":"campings"},
{"word":"campings"},{"word":"campings"},{"word":"campings"},{"word":"scheppen"}]} is of
type org.json.JSONObject at character 514 of { "galgjejson" :
03-18 09:17:40.052: W/System.err(1464): { "Nederlands" :
03-18 09:17:40.052: W/System.err(1464): { "length7" : [
03-18 09:17:40.052: W/System.err(1464): { "word" : "android" },
03-18 09:17:40.052: W/System.err(1464): { "word" : "camping" },
03-18 09:17:40.052: W/System.err(1464): { "word" : "koekjes" }
03-18 09:17:40.062: W/System.err(1464): ]
03-18 09:17:40.062: W/System.err(1464): },
03-18 09:17:40.072: W/System.err(1464): { "length8" : [
03-18 09:17:40.072: W/System.err(1464): { "word" : "androids" },
03-18 09:17:40.072: W/System.err(1464): { "word" : "campings" },
03-18 09:17:40.072: W/System.err(1464): { "word" : "campings" },
03-18 09:17:40.072: W/System.err(1464): { "word" : "campings" },
03-18 09:17:40.072: W/System.err(1464): { "word" : "campings" },
03-18 09:17:40.072: W/System.err(1464): { "word" : "campings" },
03-18 09:17:40.072: W/System.err(1464): { "word" : "scheppen" }
03-18 09:17:40.072: W/System.err(1464): ]
03-18 09:17:40.082: W/System.err(1464): },
03-18 09:17:40.092: W/System.err(1464): { "length9" : [
03-18 09:17:40.092: W/System.err(1464): { "word" : "verslapen" },
03-18 09:17:40.092: W/System.err(1464): { "word" : "versleten" },
03-18 09:17:40.102: W/System.err(1464): { "word" : "verbrande" }
03-18 09:17:40.102: W/System.err(1464): ]
03-18 09:17:40.102: W/System.err(1464): },
03-18 09:17:40.102: W/System.err(1464): { "length10" : [
03-18 09:17:40.102: W/System.err(1464): { "word" : "verslapend" },
03-18 09:17:40.102: W/System.err(1464): { "word" : "versletend" },
03-18 09:17:40.102: W/System.err(1464): { "word" : "verbranden" },
03-18 09:17:40.102: W/System.err(1464): { "word" : "verbranden" },
03-18 09:17:40.112: W/System.err(1464): { "word" : "verbranden" }
03-18 09:17:40.112: W/System.err(1464): ]
03-18 09:17:40.112: W/System.err(1464): },
03-18 09:17:40.122: W/System.err(1464): { "length11" : [
03-18 09:17:40.122: W/System.err(1464): { "word" : "verslapende" },
03-18 09:17:40.122: W/System.err(1464): { "word" : "versletende" }
03-18 09:17:40.122: W/System.err(1464): ]
03-18 09:17:40.132: W/System.err(1464): },
03-18 09:17:40.132: W/System.err(1464): { "length12" : [
03-18 09:17:40.132: W/System.err(1464): { "word" : "verslapenden" },
03-18 09:17:40.132: W/System.err(1464): ]
03-18 09:17:40.132: W/System.err(1464): }
03-18 09:17:40.142: W/System.err(1464): },
03-18 09:17:40.142: W/System.err(1464): { "English" :
03-18 09:17:40.142: W/System.err(1464): { "length7" : [
03-18 09:17:40.152: W/System.err(1464): { "word" : "leavers" },
03-18 09:17:40.152: W/System.err(1464): { "word" : "shoping" },
03-18 09:17:40.152: W/System.err(1464): { "word" : "cooking" }