Android的问题解析JSON [英] Android problems with parsing json
问题描述
您好,我想从我的网站解析JSON。特jsonString我距离Te HTT presponse得到工作正常,但是当我试图解析它在的JSONObject它提供了NPE。
这与JSON IM测试现在:
{galgjejson:
{荷兰:
{length7:[
{词:机器人},
{词:露营},
{词:koekjes}
]
},
{length8:[
{词:机器人},
{词:露营},
{词:露营},
{词:露营},
{词:露营},
{词:露营},
{词:scheppen}
]
},
{length9:[
{词:verslapen},
{词:versleten},
{词:verbrande}
]
},
{length10:[
{词:verslapend},
{词:versletend},
{词:verbranden},
{词:verbranden},
{词:verbranden}
]
},
{length11:[
{词:verslapende},
{词:versletende}
]
},
{length12:[
{词:verslapenden}
]
}
},
{ 英语 :
{length7:[
{词:毕业生},
{词:街上购物},
{词:烹饪}
]
},
{length8:[
{词:停止},
{词:沉睡},
{词:开始},
{词:开始},
{词:开始},
{词:开始},
{词:开始}
]
},
{length9:[
{词:somewordd},
{词:somewordd},
{词:somewordd}
]
},
{length10:[
{词:someworddd},
{词:someworddd}
]
},
{length11:[
{词:somewordddd},
{词:somewordddd},
{词:somewordddd},
{词:somewordddd},
{词:somewordddd}
]
},
{length12:[
{词:someworddddd},
{词:someworddddd},
{词:someworddddd}
]
} }}
我做的方法:
公共MikeyJSON(字符串jsonString){
Log.i(JSON,jsonString:+ jsonString);
尝试{
JSONObject的myObject的=新的JSONObject(jsonString);
Log.i(JSON,myObject_Object:+ myObject.toString());
}赶上(JSONException E){
// TODO自动生成catch块
e.printStackTrace();
}}公共字符串的getResult(郎字符串,字符串长度,诠释wordPos){
串字=0; 尝试{
langObject = myObject.getJSONObject(郎);
Log.i(JSON,Language_Object+ langObject.toString());
lengthObject = langObject.getJSONObject(长);
Log.i(JSON,wordlength_Object+ lengthObject.toString());
wordArray = lengthObject.getJSONArray(长);
Log.i(JSON,wordlength_Object+ wordArray.toString());
如果(wordPos> wordArray.length()){
wordPos = 0;
}
对于(i = 0; I< wordArray.length();我++){
字= wordArray.getJSONObject(wordPos)的ToString();
}
Log.i(JSON,word_Object+字); }赶上(JSONException E){
// TODO自动生成catch块
e.printStackTrace();
} 返回字;
}
}
我试图找回只有一个给定的语言和长度的字。这是我的很多研究后提出,但我只是不知道为什么它不会工作。如果我的JSON是无效的请求告诉我,我做错了什么,监守我得到这个错误之前NPE:
9月3日至十八日:17:40.052:W / System.err的(1464):org.json.JSONException:名称必须是字符串,
但{length8:[{词:机器人},{字:露营},{字:露营},
{词:露营},{字:露营},{字:露营},{字:scheppen}]}是
在{galgjejson的字符键入514 org.json.JSONObject:
9月三日至18日:17:40.052:W / System.err的(1464):{荷兰:
9月三日至18日:17:40.052:W / System.err的(1464):{length7:[
9月三日至18日:17:40.052:W / System.err的(1464):{词:机器人},
9月三日至18日:17:40.052:W / System.err的(1464):{词:露营},
9月三日至18日:17:40.052:W / System.err的(1464):{词:koekjes}
9月三日至18日:17:40.062:W / System.err的(1464):]
9月三日至18日:17:40.062:W / System.err的(1464):},
9月三日至18日:17:40.072:W / System.err的(1464):{length8:[
9月三日至18日:17:40.072:W / System.err的(1464):{词:机器人},
9月三日至18日:17:40.072:W / System.err的(1464):{词:露营},
9月三日至18日:17:40.072:W / System.err的(1464):{词:露营},
9月三日至18日:17:40.072:W / System.err的(1464):{词:露营},
9月三日至18日:17:40.072:W / System.err的(1464):{词:露营},
9月三日至18日:17:40.072:W / System.err的(1464):{词:露营},
9月三日至18日:17:40.072:W / System.err的(1464):{词:scheppen}
9月三日至18日:17:40.072:W / System.err的(1464):]
9月三日至18日:17:40.082:W / System.err的(1464):},
9月三日至18日:17:40.092:W / System.err的(1464):{length9:[
9月三日至18日:17:40.092:W / System.err的(1464):{词:verslapen},
9月三日至18日:17:40.092:W / System.err的(1464):{词:versleten},
9月三日至18日:17:40.102:W / System.err的(1464):{词:verbrande}
9月三日至18日:17:40.102:W / System.err的(1464):]
9月三日至18日:17:40.102:W / System.err的(1464):},
9月三日至18日:17:40.102:W / System.err的(1464):{length10:[
9月三日至18日:17:40.102:W / System.err的(1464):{词:verslapend},
9月三日至18日:17:40.102:W / System.err的(1464):{词:versletend},
9月三日至18日:17:40.102:W / System.err的(1464):{词:verbranden},
9月三日至18日:17:40.102:W / System.err的(1464):{词:verbranden},
9月三日至18日:17:40.112:W / System.err的(1464):{词:verbranden}
9月三日至18日:17:40.112:W / System.err的(1464):]
9月三日至18日:17:40.112:W / System.err的(1464):},
9月三日至18日:17:40.122:W / System.err的(1464):{length11:[
9月三日至18日:17:40.122:W / System.err的(1464):{词:verslapende},
9月三日至18日:17:40.122:W / System.err的(1464):{词:versletende}
9月三日至18日:17:40.122:W / System.err的(1464):]
9月三日至18日:17:40.132:W / System.err的(1464):},
9月三日至18日:17:40.132:W / System.err的(1464):{length12:[
9月三日至18日:17:40.132:W / System.err的(1464):{词:verslapenden},
9月三日至18日:17:40.132:W / System.err的(1464):]
9月三日至18日:17:40.132:W / System.err的(1464):}
9月三日至18日:17:40.142:W / System.err的(1464):},
9月三日至18日:17:40.142:W / System.err的(1464):{英语:
9月三日至18日:17:40.142:W / System.err的(1464):{length7:[
9月三日至18日:17:40.152:W / System.err的(1464):{词:毕业生},
9月三日至18日:17:40.152:W / System.err的(1464):{词:SHOPING},
9月三日至18日:17:40.152:W / System.err的(1464):{词:烹饪}
您JSON是无效的 - 这是正确的格式:
{荷兰:[
{length7:[
{词:机器人},
{词:露营},
{词:koekjes}
]
},
{length8:[
{词:机器人},
{词:露营},
{词:露营},
{词:露营},
{词:露营},
{词:露营},
{词:scheppen}
]
}]
}
目前,荷兰是一个JSONArray格式(它持有多个一个JSONObjects),但你的回答是缺少方括号。如果您解析荷兰作为JSONArray和一个删除长度一个JSONObjects之一,那么你应该能够使它工作
Hello I'm trying to parse json from my website. Te jsonString I get from te httpresponse is working fine but when I try to parse it in an JSONObject it gives a NPE. This is the json im testing with right now:
{ "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" }
]
}
}
}
The method I made:
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;
}
}
What I'm trying to retrieve is only one word of the given language and length. This is what I made after alot of research but i just don't get why it won't work. If my json is invalid pleas tell me what i'm doing wrong, becuase I get this error before the 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" }
Your JSON is invalid - this is the correct format:
{ "Nederlands" : [
{ "length7" : [
{ "word" : "android" },
{ "word" : "camping" },
{ "word" : "koekjes" }
]
},
{ "length8" : [
{ "word" : "androids" },
{ "word" : "campings" },
{ "word" : "campings" },
{ "word" : "campings" },
{ "word" : "campings" },
{ "word" : "campings" },
{ "word" : "scheppen" }
]
}]
}
At the moment, "Nederlands" is in the format of a JSONArray (it's holding multiple JSONObjects) but your response is missing the square brackets. If you parse "Nederlands" as a JSONArray and remove the "length" JSONObjects one by one then you should be able to make it work
这篇关于Android的问题解析JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!