Java循环对象

时间:2017-09-17 09:23:55

标签: java arrays json

我是Java的新手 - 我正在尝试循环使用JSONArray创建一个“标签”,“值”数组。

JSONArray records = (JSONArray) sch.get("schData");

看起来像这样A)

{"emotional distress":4,"peer difficulties":6,"behavioural difficulties":8,"kind and helpful behaviour":8,"overall stress":32,"_sdqID":11,"hyperactivity and concentration":6}

我希望遍历此对象以创建以下结构

B)

"chart": [{
                  "label": "Overall Stress",
                  "value": 89
                },{
                  "label": "Emotional Stress",
                  "value": 1
                },{
                  "label": "Behavioural difficulties",
                  "value": 29
                },{
                  "label": "hyperactivity and concetration",
                  "value": 89
                },{
                  "label": "Getting along with others",
                  "value": 19
                },{
                  "label": "Keen and helpful behaviour",
                  "value": 99
                }]

-

因此,当我创建一个记录时,我想要对密钥进行标题化 - 而不是包含_sdqID元素。我该怎么做?

我可以尝试手动创建一些东西。

    JSONObject row = new JSONObject();

    row.put("label", "Emotional Distress");
    row.put("value", ((JSONObject) records.get(i)).get("emotional distress"));

    rowArray.add(row);

我尝试把它放在第二个循环中 - 但我开始在其中发现强制转换问题。所以我不确定这一步的最佳方法是什么。

for (int j = 0; j < ((JSONObject) records.get(i)).size(); j++) {
//code
}

4 个答案:

答案 0 :(得分:0)

您可以直接获取JSON数组,然后循环访问它。

import org.json.*;

public class JsonIO {

   public static void parseJson(StringBuffer sb){
   // sb is the JSON string
      JSONObject obj = new JSONObject(sb);

      JSONArray arr = obj.getJSONArray("chart");

      for (int i = 0; i< arr.length(); i++){
         // loop through
         System.out.println(arr.getJSONObject(i).getString("label")); // i.e
         System.out.println(arr.getJSONObject(i).getString("value"));
      }
   }

}

其次,GSON lib。可以使用。您可以download

public static void parseJson(String sb){

 JsonParser jsonParser = new JsonParser();
 JsonObject jo = (JsonObject)jsonParser.parse(sb);
 JsonArray jArray = jo.getAsJsonArray("chart"); // get json array

 Gson gJson = new Gson();
 ArrayList jsonObjArrayList = gJson.fromJson(jArray, ArrayList.class);


 for (int i = 0; i< jsonObjArrayList.size(); i++){
    System.out.println(jsonObjArrayList.get(i).toString());
 }
}

答案 1 :(得分:0)

public static void main(String[] args) {
    // TODO Auto-generated method stub
    JSONObject obj = new JSONObject();
    obj.put("emotional distress", 4);
    obj.put("peer difficulties", 6);
    obj.put("behavioural difficulties", 9);
    JSONArray array = new JSONArray();
    for(String key : obj.keySet()) {
        JSONObject newObj = new JSONObject();
        newObj.put("label", key);
        newObj.put("value", obj.get(key));
        array.put(newObj);
    }
    System.out.println(array);
}

您的A对象在此obj,B为array

答案 2 :(得分:0)

您可以使用此代码进行一些修改。我已将输出中的键更改为大写,您可以根据需要编写一些。

public static void main(String[] args) {

    String data = "{\"emotional distress\":4,\"peer difficulties\":6,\"behavioural difficulties\":8,\"kind and helpful behaviour\":8,\"overall stress\":32,\"_sdqID\":11,\"hyperactivity and concentration\":6}";

    JSONObject json = new JSONObject();
    JSONArray chart = new JSONArray();
    json.put("chart", chart);
    JSONObject jsonObject = new JSONObject(data);
    Iterator<String> iterator = jsonObject.keys();
    while(iterator.hasNext()) {
        String key = iterator.next();
        if(key.equalsIgnoreCase("_sdqID")) {
            continue;
        }
        int value = jsonObject.getInt(key);
        key = key.toUpperCase();
        JSONObject row = new JSONObject();
        row.put("label", key);
        row.put("value", value);
        chart.put(row);
    }
}

答案 3 :(得分:0)

阿兰卡你修好了男人,欢呼。 obj.keySet()。iterator();“这将解决问题。这就是我必须做的事情来循环内部数据。

好的,所以变量sch看起来像这样

{"schData":[{"emotional distress":4,"peer difficulties":6,"behavioural difficulties":8,"kind and helpful behaviour":8,"overall stress":32,"_sdqID":11,"hyperactivity and concentration":6},{"emotional distress":4,"peer difficulties":8,"behavioural difficulties":6,"kind and helpful behaviour":5,"overall stress":28,"_sdqID":10,"hyperactivity and concentration":5},{"emotional distress":4,"peer difficulties":8,"behavioural difficulties":6,"kind and helpful behaviour":5,"overall stress":28,"_sdqID":9,"hyperactivity and concentration":5},{"emotional distress":2,"peer difficulties":2,"behavioural difficulties":4,"kind and helpful behaviour":2,"overall stress":13,"_sdqID":8,"hyperactivity and concentration":3},{"emotional distress":5,"peer difficulties":6,"behavioural difficulties":8,"kind and helpful behaviour":7,"overall stress":32,"_sdqID":7,"hyperactivity and concentration":6},{"emotional distress":7,"peer difficulties":6,"behavioural difficulties":8,"kind and helpful behaviour":9,"overall stress":34,"_sdqID":6,"hyperactivity and concentration":4},{"emotional distress":5,"peer difficulties":4,"behavioural difficulties":4,"kind and helpful behaviour":6,"overall stress":21,"_sdqID":5,"hyperactivity and concentration":2},{"emotional distress":1,"peer difficulties":0,"behavioural difficulties":0,"kind and helpful behaviour":0,"overall stress":1,"_sdqID":4,"hyperactivity and concentration":0}]}

这是java代码

JSONArray data = new JSONArray();

//System.out.println("sch" + sch);

JSONArray records  = (JSONArray) sch.get("schData");

for (int i = 0; i < records.size(); i++) {
    //code

    JSONObject chart = new JSONObject();

    JSONObject obj = (JSONObject) records.get(i);

    JSONArray rowArray = new JSONArray();
    for (Object key : obj.keySet()) {
        String keyStr = (String)key;
        Object keyvalue = obj.get(keyStr);

        //Print key and value
       // System.out.println("key: "+ keyStr + " value: " + keyvalue);

        JSONObject row = new JSONObject();


        row.put("label", keyStr);
        row.put("value", keyvalue);

        rowArray.add(row);
    }
    chart.put("chart", rowArray);



    JSONObject chartRecord = new JSONObject();
    chartRecord.put("title", "xxx");
    chartRecord.put("contents", chart);

    data.add(chartRecord);
}



//System.out.println("chart data: "+ data);

---响应是这个

[{
    "contents": {
        "chart": [{
            "label": "emotional distress",
            "value": 4
        }, {
            "label": "peer difficulties",
            "value": 6
        }, {
            "label": "behavioural difficulties",
            "value": 8
        }, {
            "label": "kind and helpful behaviour",
            "value": 8
        }, {
            "label": "overall stress",
            "value": 32
        }, {
            "label": "_sdqID",
            "value": 11
        }, {
            "label": "hyperactivity and concentration",
            "value": 6
        }]
    },
    "title": "xxx"
}, {
    "contents": {
        "chart": [{
            "label": "emotional distress",
            "value": 4
        }, {
            "label": "peer difficulties",
            "value": 8
        }, {
            "label": "behavioural difficulties",
            "value": 6
        }, {
            "label": "kind and helpful behaviour",
            "value": 5
        }, {
            "label": "overall stress",
            "value": 28
        }, {
            "label": "_sdqID",
            "value": 10
        }, {
            "label": "hyperactivity and concentration",
            "value": 5
        }]
    },
    "title": "xxx"
}, {
    "contents": {
        "chart": [{
            "label": "emotional distress",
            "value": 4
        }, {
            "label": "peer difficulties",
            "value": 8
        }, {
            "label": "behavioural difficulties",
            "value": 6
        }, {
            "label": "kind and helpful behaviour",
            "value": 5
        }, {
            "label": "overall stress",
            "value": 28
        }, {
            "label": "_sdqID",
            "value": 9
        }, {
            "label": "hyperactivity and concentration",
            "value": 5
        }]
    },
    "title": "xxx"
}, {
    "contents": {
        "chart": [{
            "label": "emotional distress",
            "value": 2
        }, {
            "label": "peer difficulties",
            "value": 2
        }, {
            "label": "behavioural difficulties",
            "value": 4
        }, {
            "label": "kind and helpful behaviour",
            "value": 2
        }, {
            "label": "overall stress",
            "value": 13
        }, {
            "label": "_sdqID",
            "value": 8
        }, {
            "label": "hyperactivity and concentration",
            "value": 3
        }]
    },
    "title": "xxx"
}, {
    "contents": {
        "chart": [{
            "label": "emotional distress",
            "value": 5
        }, {
            "label": "peer difficulties",
            "value": 6
        }, {
            "label": "behavioural difficulties",
            "value": 8
        }, {
            "label": "kind and helpful behaviour",
            "value": 7
        }, {
            "label": "overall stress",
            "value": 32
        }, {
            "label": "_sdqID",
            "value": 7
        }, {
            "label": "hyperactivity and concentration",
            "value": 6
        }]
    },
    "title": "xxx"
}, {
    "contents": {
        "chart": [{
            "label": "emotional distress",
            "value": 7
        }, {
            "label": "peer difficulties",
            "value": 6
        }, {
            "label": "behavioural difficulties",
            "value": 8
        }, {
            "label": "kind and helpful behaviour",
            "value": 9
        }, {
            "label": "overall stress",
            "value": 34
        }, {
            "label": "_sdqID",
            "value": 6
        }, {
            "label": "hyperactivity and concentration",
            "value": 4
        }]
    },
    "title": "xxx"
}, {
    "contents": {
        "chart": [{
            "label": "emotional distress",
            "value": 5
        }, {
            "label": "peer difficulties",
            "value": 4
        }, {
            "label": "behavioural difficulties",
            "value": 4
        }, {
            "label": "kind and helpful behaviour",
            "value": 6
        }, {
            "label": "overall stress",
            "value": 21
        }, {
            "label": "_sdqID",
            "value": 5
        }, {
            "label": "hyperactivity and concentration",
            "value": 2
        }]
    },
    "title": "xxx"
}, {
    "contents": {
        "chart": [{
            "label": "emotional distress",
            "value": 1
        }, {
            "label": "peer difficulties",
            "value": 0
        }, {
            "label": "behavioural difficulties",
            "value": 0
        }, {
            "label": "kind and helpful behaviour",
            "value": 0
        }, {
            "label": "overall stress",
            "value": 1
        }, {
            "label": "_sdqID",
            "value": 4
        }, {
            "label": "hyperactivity and concentration",
            "value": 0
        }]
    },
    "title": "xxx"
}]