How to get and set JSONObject , JSONArray in J2ME

无人久伴 提交于 2019-12-06 19:53:23

Check this link for different JSON Data Set Sample

One Example for your Understanding::: JSON String Nested with Arrays

{
    "id": "0001",
    "type": "donut",
    "name": "Cake",
    "ppu": 0.55,
    "batters":
        {
            "batter":
                [
                    { "id": "1001", "type": "Regular" },
                    { "id": "1002", "type": "Chocolate" },
                    { "id": "1003", "type": "Blueberry" },
                    { "id": "1004", "type": "Devil's Food" }
                ]
        },
    "topping":
        [
            { "id": "5001", "type": "None" },
            { "id": "5002", "type": "Glazed" },
            { "id": "5005", "type": "Sugar" },
            { "id": "5007", "type": "Powdered Sugar" },
            { "id": "5006", "type": "Chocolate with Sprinkles" },
            { "id": "5003", "type": "Chocolate" },
            { "id": "5004", "type": "Maple" }
        ]
}

To check its valid or not check this link (JSON Validator)

To check JSON Viewer

So Here is code take look::

String json = "{\"id\":\"0001\",\"type\":\"donut\",\"name\":\"Cake\""
                + ",\"ppu\":0.55,\"batters\":{\"batter\":["
                + "{\"id\":\"1001\",\"type\":\"Regular\"},{\"id\":\"1002\","
                + "\"type\":\"Chocolate\"},{\"id\":\"1003\","
                + "\"type\": \"Blueberry\" },{ \"id\": \"1004\", "
                + "\"type\": \"Devil's Food\" } ] },"
                + " \"topping\":["
                + "{ \"id\": \"5001\", \"type\": \"None\" },"
                + "{ \"id\": \"5002\", \"type\": \"Glazed\" },"
                + "{ \"id\": \"5005\", \"type\": \"Sugar\" },"
                + "{ \"id\": \"5007\", \"type\": \"Powdered Sugar\" },"
                + " { \"id\": \"5006\", \"type\": \"Chocolate with Sprinkles\" },"
                + "{ \"id\": \"5003\", \"type\": \"Chocolate\" },"
                + "{ \"id\": \"5004\", \"type\": \"Maple\" }]}";
        try {
            JSONObject root = new JSONObject(json);
            String id = root.getString("id");
            double dd = root.getDouble("ppu");

            System.out.println(""+id);
            System.out.println(""+dd);

            JSONObject batters=new JSONObject(root.getString("batters"));
            JSONArray batter=new JSONArray(batters.getString("batter"));

            for(int j=0;j<batter.length();j++){
                JSONObject navgt_batter=new JSONObject(batter.getString(j));
                 String id_batter= navgt_batter.getString("id");
                String type_batter=navgt_batter.getString("type");
                  System.out.println(""+id+" "+type_batter);
            }

            JSONArray topping=root.getJSONArray("topping");
             for(int k=0;k<topping.length();k++){
                 JSONObject navgt_batter=new JSONObject(topping.getString(k));
                 String id_top =navgt_batter.getString("id");
                String type_top=navgt_batter.getString("type");
                 System.out.println(""+id_top+" "+type_top);
             }

        } catch (JSONException ex) {
            ex.printStackTrace();
        }

You can use your same concept to set & get data like above you did. complex data structure always easy to handle in JSON, don't worry about it. Thanks

In the below link http://jimmod.com/blog/2011/09/java-me-j2me-json-implementation-for-array-object/

they have explained how a JSONArray is used

public void fromJSON(String jsonString) {
        try {
            JSONObject json = new JSONObject(jsonString);
            setApi_status(json.getString("api_status"));
            JSONArray jsonArray = json.getJSONArray("threads");
            int total = jsonArray.length();
            ThreadData[] threads = new ThreadData[total];
            for (int i=0;i<total;i++) {
                String threadsJSON = jsonArray.getString(i);
                threads[i] = new ThreadData();
                threads[i].fromJSON(threadsJSON);
            }
            setThreads(threads);
        } catch (JSONException ex) {
            ex.printStackTrace();
        }
    }
    public String toJSON() {
        JSONObject inner = new JSONObject();
        try {
            inner.put("api_status", getApi_status());
            JSONArray jsonArray = new JSONArray();
            ThreadData[] threads = getThreads();
            for (int i=0;i<threads.length;i++) {
                jsonArray.put(threads[i].toJSON());
            }
            inner.put("threads", jsonArray);
        } catch (JSONException ex) {
            ex.printStackTrace();
        }
        return inner.toString();
    }

Where Threaddata is a class defined for a JSONObject and it has been made in array object check out the link

It pretty the same way .. all you need is to just loop through the array ... i added tags to your sample JSON data

    String sample = "{\"id\":99,\"name\":\"Tester\",\"description\":\"This is JSON Data\",\"tags\":[\"eat\",\"swim\",\"sleep\"]}";
    try {
        JSONObject objSample = new JSONObject(sample);
        JSONArray array = new JSONArray(objSample.getJSONArray("tags").toString());
        System.out.println(objSample.get("id").toString());
        System.out.println(objSample.get("name").toString());
        System.out.println(objSample.get("description").toString());
        for (int i = 0; i < array.length(); i++) {
            System.out.println(array.get(i).toString());
        }

    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

Output

    99
    Tester
    This is JSON Data
    eat
    swim
    sleep

I hope this helps

Thanks :)

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