1

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" i.e 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"}]

I want to access the data from both "titles" and "values" i.e Event 2 should also be displayed. 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();
    }
}
4

1 Answer 1

0

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();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.