Android的问题解析JSON(Android problems with parsing jso

2019-10-17 20:43发布

你好,我想从我的网站解析JSON。 特jsonString我从TE的HttpResponse获取工作正常,但是当我试图解析它在的JSONObject它提供了NPE。 这与现在的JSON IM测试:

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

Answer 1:

您的JSON是无效的 - 这是正确的格式:

{ "Nederlands" :  [
                        { "length7" : [ 
                                            { "word" : "android" }, 
                                            { "word" : "camping" },
                                            { "word" : "koekjes" }
                                        ]
                        }, 
                        { "length8" : [
                                            { "word" : "androids" },    
                                            { "word" : "campings" },
                                            { "word" : "campings" },
                                            { "word" : "campings" },
                                            { "word" : "campings" },
                                            { "word" : "campings" },
                                            { "word" : "scheppen" }
                                        ]
                        }]
}

目前,“荷兰”是一个JSONArray格式(它所持有多一个JSONObjects),但你的反应缺少方括号。 如果您解析“荷兰”作为JSONArray和一个删除“长度”一个JSONObjects之一,那么你应该能够使它工作



Answer 2:

尝试实体转换为字符串如下然后用杰森对象

  response = httpclient.execute(httpget);

HttpEntity entity = response.getEntity();

String responseBody = EntityUtils.toString(entity);

  JSONObject obj = new JSONObject(responseBody);


文章来源: Android problems with parsing json