简体   繁体   中英

Android problems with parsing json

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

Try converting the entity to string as follows then use the jason object

  response = httpclient.execute(httpget);

HttpEntity entity = response.getEntity();

String responseBody = EntityUtils.toString(entity);

  JSONObject obj = new JSONObject(responseBody);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM