简体   繁体   English

在Java中访问多个json对象

[英]Access multiple json objects in java

I am trying to access data from Json in Java code, I have written the java code, while trying to access the "tittle" or "value" in json, I am getting only one value of "title" ie Event 1 also when I try to access "values" using list I get data like [{"0":"1_a","1":"1_b"}, {"0":"2_a","1":"2_b"}] 我正在尝试使用Java代码从Json访问数据,我编写了Java代码,同时尝试访问json中的“标题”或“值”,但是当我获取“ title”(即事件1)的一个值时,尝试使用列表访问“值”,我得到类似[{"0":"1_a","1":"1_b"}, {"0":"2_a","1":"2_b"}]

I want to access the data from both "titles" and "values" ie Event 2 should also be displayed. 我想从“标题”和“值”访问数据,即事件2也应显示。 All the imports have been made. 全部进口。 Code Here 在这里编码

 public class JsonToJava {

    public void JsontoString() {

        String title;
        String jsonString = "{\"title\":\"Event 1\","
                + "\"param\":[\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\"],"
                + "\"status\":true,"
                + "\"values\":[{"
                + "\"0\":{\"0\":\"1_a\",\"1\":\"1_b\"},"
                + "\"1\":{\"0\":\"2_a\",\"1\":\"2_b\"}}]"
                + ",\"$$hashKey\":\"object:3\"}"
                + ",{\"title\":\"Event 2\","
                + "\"param\":[\"1\",\"2\",\"3\",\"4\",\"Price1\",\"Price2\",\"5\",\"Status\"],"
                + "\"status\":true," + "\"values\":[{"
                + "\"0\":{\"0\":\"A_a\",\"1\":\"A_b\"},"
                + "\"1\":{\"0\":\"B_a\",\"1\":\"B_b\"}}]"
                + ",\"$$hashKey\":\"object:4\"}";

        try {
            title = new JSONObject(jsonString).getString("title");

            System.out.println(title);

            // JSONObject obj = new
            // JSONObject("{"0":{"0":"1_a","1":"1_b"},"1":{"0":"2_a","1":"2_b"}}");

            JSONObject obj1 = new JSONObject(jsonString);

            List<String> list = new ArrayList<String>();
            JSONArray array = obj1.getJSONArray("values");

            String val = array.getString(0);

            for (int i = 0; i < array.length(); i++) {
                list.add(array.getJSONObject(i).getString("0"));
                list.add(array.getJSONObject(i).getString("1"));
            }

            System.out.println(list);

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        JsonToJava js = new JsonToJava();

        js.JsontoString();
    }
}

Try using this code 尝试使用此代码

public void JsontoString() {
    String jsonString = "{\"root\":[{\"title\":\"Event 1\"," + "\"param\":[\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\"]," + "\"status\":true," + "\"values\":[{"
            + "\"0\":{\"0\":\"1_a\",\"1\":\"1_b\"}," + "\"1\":{\"0\":\"2_a\",\"1\":\"2_b\"}}]" + ",\"$$hashKey\":\"object:3\"}"
            + ",{\"title\":\"Event 2\"," + "\"param\":[\"1\",\"2\",\"3\",\"4\",\"Price1\",\"Price2\",\"5\",\"Status\"]," + "\"status\":true,"
            + "\"values\":[{" + "\"0\":{\"0\":\"A_a\",\"1\":\"A_b\"}," + "\"1\":{\"0\":\"B_a\",\"1\":\"B_b\"}}]" + ",\"$$hashKey\":\"object:4\"}]}";
    try {
        System.out.println(jsonString);
        JSONObject obj1 = new JSONObject(jsonString);

        List<String> list = new ArrayList<String>();
        JSONArray array = obj1.getJSONArray("root");
        for (int i = 0; i < array.length(); i++) {
            list.add(array.getJSONObject(i).getString("title"));
        }
        System.out.println(list);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM