Help parsing simple JSON (using JSON for JAVA ME)

删除回忆录丶 提交于 2019-12-24 18:33:54

问题


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



来源:https://stackoverflow.com/questions/5303859/help-parsing-simple-json-using-json-for-java-me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!