java - 如何在 J2ME 中获取和设置 JSONObject , JSONArray

标签 java arrays json java-me midp

我是 J2ME 中 JSON 编程的新手。

我发现 Json 被用来像 XML 一样交换数据。

我想知道从 JSONtoObject 到 Array 对象的交换,反之亦然

下面写的是我从 JSON 转换为对象的代码,反之亦然。

但我不知道如何处理复杂的数据结构,如数组等。

//应用加载器

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;


public class AppLoader extends MIDlet {

    public AppLoader() {
        // TODO Auto-generated constructor stub

        // Converting Object to JSON

        UserData data=new UserData();
        data.setId(10);
        data.setName("Yatin");
        data.setDescription("Testing JSON in J2ME");
        System.out.println("Convert to JSON"+data.toJSON());


        //Convert JSON to Object
        String sample="{\"id\":99,\"name\":\"Tester\",\"description\":\"This is JSON Data\"}";
        UserData data2=new UserData();
        data2.fromJSON(sample);
        System.out.println("Convert from JSON "+data2);
    }

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
        // TODO Auto-generated method stub

    }

    protected void pauseApp() {
        // TODO Auto-generated method stub

    }

    protected void startApp() throws MIDletStateChangeException {
        // TODO Auto-generated method stub

    }

}

在这个类中,我为 String 类型的对象创建了 getter 和 setter,然后创建了 JsonObject 以创建 JSON 对象以创建 JSON 对象,然后根据函数 toJSON()来自JSON()

//用户数据类

import org.json.me.JSONException;
import org.json.me.JSONObject;


public class UserData {
    private int id;
    private String name;
    private String description;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String toString()
    {
        return getId()+"-"+getName()+"-"+getDescription();
    }



    public String toJSON() {
        // TODO Auto-generated method stub
        JSONObject inner=new JSONObject();

        try {
            inner.put("id",getId());
            inner.put("description", getDescription());
            inner.put("name", getName());
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return inner.toString();
    }

    public void fromJSON(String jsonString) {
        // TODO Auto-generated method stub
        try {
            JSONObject json=new JSONObject(jsonString);
            setId(json.getInt("id"));
            setDescription(json.getString("description"));
            setName(json.getString("name"));
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



    }

}

我为这个问题找到了更好的链接

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

最佳答案

Check this link for different JSON Data Set Sample

一个例子让你理解:::嵌套在数组中的 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" }
        ]
}

检查其是否有效check this link (JSON Validator)

检查JSON Viewer

所以这里是代码看看::

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();
        }

您可以像上面那样使用相同的概念来设置和获取数据。复杂的数据结构在 JSON 中总是很容易处理,不用担心。谢谢

关于java - 如何在 J2ME 中获取和设置 JSONObject , JSONArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9871725/

相关文章:

C# 模拟 xmlhttprequest 并获取 json 响应

json - IE9 JSON 数据 "do you want to open or save this file"

java - 为什么从非同步集合读取时会发生 ConcurrentModificationException

java - 如何安装 Java 7 EE SDK 下载为 Mac OSX 的 .sh 文件

javascript - 在html表格中显示javascript数组

javascript - 使用 for 循环设置数组中每个元素之间的延迟

php - 用 json 返回一个 blob

java - 多线程中的对象锁定

java - 如何使用 jersey 将 header 信息作为键值对传递以使用休息服务

C++将值传递给函数中的二维字符数组