Help parsing simple JSON (using JSON for JAVA ME)

2019-07-22 17:37发布

问题:

I new to parsing JSON - up until this point, I've been purely XML. Anyways, I am using JSON (Java ME) to parse something with the following structure:

{"name" : "JACK","name" : "JILL","name" : "JOHN","name" : "JENNY","name" : "JAMES","name" : "JIM"}

Here is my code:

    try {
        JSONObject json = new JSONObject(response);
        JSONArray jsonArray = outer.getJSONArray("name");
        System.out.println("ARRAY SIZE:"
                + jsonArray.length());
    } catch (JSONException ex) {
    }

My problem is that I cannot even get the println("ARRAY SIZE:"...) statement to output at all in my Eclipse console. The only time that I am getting any sort of output is if I use the following code:

try {
        JSONObject json = new JSONObject(response);
        System.out.println("OUTPUT:"
                + json.getString("name"));
    } catch (JSONException ex) {
    }

...That seems to give me only the last element. Is there a reason why I cannot get the JSONArray to work? Is it because the JSON contains no "outer" key?

I'd appreciate any help. Thanks!

回答1:

The key must be unique. You need to differentiate those "name"'s by using "Name1", "Name2", ... etc



回答2:

Hey Hi create one json file like format.... {"name1" : "JACK","name2" : "JILL","name3" : "JOHN","name4" : "JENNY","name5" : "JAMES","name6" : "JIM"} & save this file in WEB-INF Folder on server & get response from server with reading this file... Thanks