【问题标题】:Combine json and make arraylist<object>结合json并制作arraylist<object>
【发布时间】:2016-04-25 07:15:02
【问题描述】:

我有 JSON 我想将第一个对象的数据合并到第二个对象中

{
    "ViewId": {
        "56": {
            "ViewId": "56",
            "Name": "hi",
            "param": "value"
        },
        "88": {
            "ViewId": "88",
            "Name": "hi2",
            "param": "value2"
        }
    },
    "que": [
        {
            "RId": "123",
            "ViewId": "88",
            "Count": 0
        },
        {
            "RId": "456",
            "ViewId": "56",
            "Count": 0
        }
    ]
}

基本上,我正在制作 ArrayList,如何将 ViewId 数据添加到 que。 我想通过以下方式合并 JSON:

{
    "que": [
        {
            "RId": "123",
            "ViewId": "88",
            "Name": "hi2",
            "param": "value2",
            "Count": 0
        },
        {
            "RId": "456",
            "ViewId": "56",
            "Name": "hi",
            "param": "value",
            "Count": 0
        }
    ]
}

【问题讨论】:

  • 不清楚你想要什么。请尝试使您的问题更正确。请使用逗号和大写字母。
  • 您可以为此运行一个循环并检查 viewId 数组中的相应 JSONObject 并对其进行修改。
  • 是否要将viewId的元素合并到que中?像 viewId 的第一个元素应该合并到 que 的第一个元素中吧?
  • 是的,我想合并,但根据 der ViewId,例如 ViewId 为 88 应该添加到 view id 为 88 的 que 中
  • 您能否添加有关您正在使用的 JSON 库的信息? (如果有的话?)

标签: android json arraylist


【解决方案1】:
    JSONObject ViewIdJsnObject = new JSONObject(); //replace new JSONObject() with ViewId Json Object here
    JSONArray queArray = new JSONArray();//replace new JSONArray() with actual json array;

    //Traverse through all que objects in array
    if(queArray != null && queArray.length() > 0){
        for(int i=0; i<queArray.length(); i++){
            try {
                JSONObject queObj = queArray.getJSONObject(i);
                String queViewId = queObj.getString("ViewId"); //ViewId of que object at position i
                JSONObject viewIdObj = ViewIdJsnObject.getJSONObject(queViewId); //get json object against ViewId
                if(viewIdObj != null) {
                    //Now add these value to que object at position i
                    String name = viewIdObj.getString("Name");
                    String param = viewIdObj.getString("param");
                    queObj.put("Name", name);
                    queObj.put("param", param);
                }
            } catch (JSONException jse) {
                jse.printStackTrace();
            }
        }
    }
    //Now que array contains final merged data, convert it to ArrayList<Your_model>.

【讨论】:

  • queObj 没有添加它总是会与数据重叠
  • 你应该直接将它添加到你的数组列表中。先添加 que 对象,再添加对应的名称和参数值
  • JSONArray finalValue = new JSONArray();然后在 for 循环中添加到 finalValue.put(queObj) 。 finalValue 将包含所有数据
【解决方案2】:

创建一个类

public class Data {
    int id;
    List<Que> que = new ArrayList<Que>();


    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public List<Que> getQue() {
        return que;
    }
    public void setQue(List<Que> que) {
        this.que = que;
    }

}

创建另一个名为Que的类

public class Que {
    int RId;
    int ViewId;
    int Count;


    public int getrId() {
        return RId;
    }
    public void setrId(int rId) {
        this.RId = rId;
    }
    public int getViewId() {
        return ViewId;
    }
    public void setViewId(int viewId) {
        this.ViewId = viewId;
    }
    public int getCount() {
        return Count;
    }
    public void setCount(int count) {
        this.Count = count;
    }

}

通过gson使用它

Gson gson = new Gson();
        Data data = gson.fromJson(json, Data.class);
        List<Que> queList = data.getQue();
        for(Que que : queList){
            System.out.println("This is R ID" +que.RId);
            System.out.println("This is View ID" +que.ViewId);
            System.out.println("This is Count" +que.Count);

确保您的 json 属性名称与 java 实例参数匹配。

【讨论】:

  • @andro 这行得通。对不起,我偷懒了。您必须找出其他 json 字段,例如上面的 "que",即 "ViewId": "56": { .... },....... },
猜你喜欢
  • 2021-12-02
  • 1970-01-01
  • 2014-10-03
  • 2014-05-09
  • 1970-01-01
  • 1970-01-01
  • 2014-07-31
  • 2019-04-09
  • 1970-01-01
相关资源
最近更新 更多