0

I have a JSON data which is parsed to an Android App.

{"tag":"Login","tdata":[{"ID":[["C1","C2"]],"Name":[["ABC","PQR"]]}]}

I want to extract the data C1, C2, ABC, PQR in separate string variables. How can I do it?

When I tried the following code:

JSONObject json=data.getJsonArray(0);
JSONArray id=json.getJSONArray("ID");
System.out.println(id);

This was the output:

[["C1","C2"]]
4
  • 2
    have you tried some thing? Commented Aug 31, 2014 at 16:05
  • read here... androidhive.info/2012/01/android-json-parsing-tutorial Commented Aug 31, 2014 at 16:11
  • @Aneesh answer posted for your acceptance Commented Aug 31, 2014 at 16:36
  • @ankur-singhal it works as a gem! Commented Aug 31, 2014 at 17:15

3 Answers 3

1

I think beauty is in simplicity so look at this:

String str = "{\"tag\":\"Login\",\"tdata\":[{\"ID\":[[\"C1\",\"C2\"]],\"Name\":[[\"ABC\",\"PQR\"]]}]}";
    System.out.println(str);
    JSONObject all;
    try {
        all = new JSONObject(str);
        JSONArray data =  all.getJSONArray("tdata");              //data = [{"ID":[["C1","C2"]],"Name":[["ABC","PQR"]]}]
        JSONObject insideData=data.getJSONObject(0);              // insideData = {"ID":[["C1","C2"]],"Name":[["ABC","PQR"]]}
        JSONArray C1C2OutterArray=insideData.getJSONArray("ID");  // C1C2OutterArray = [["C1","C2"]]
        JSONArray C1C2InnerArray=C1C2OutterArray.getJSONArray(0); // C1C2InnerArray = ["C1","C2"]   
        String C1 = C1C2InnerArray.getString(0);                  // C1 = C1
        String C2 = C1C2InnerArray.getString(1);                  // C2 = C2

        JSONArray nameOutterArray=insideData.getJSONArray("Name");// nameOutterArray = [["ABC","PQR"]]
        JSONArray nameInnerArray=nameOutterArray.getJSONArray(0); // nameInnerArray = ["ABC","PQR"] 
        String ABC = nameInnerArray.getString(0);                 // ABC = ABC
        String PQR = nameInnerArray.getString(1);                 // PQR = PQR

    } catch (JSONException e) {  

        e.printStackTrace();
    }
Sign up to request clarification or add additional context in comments.

Comments

1

working code

package com.test;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

public class Test{

    public static void main(String[] args) {
        String s = "{\"tag\":\"Login\",\"tdata\":[{\"ID\":[[\"C1\",\"C2\"]],\"Name\":[[\"ABC\",\"PQR\"]]}]}";
        System.out.println("=>" + s);

        JSONObject json;
        try {
            json = new JSONObject(s);
            JSONArray jsonArray = json.getJSONArray("tdata");
            for (int i = 0 ; i < jsonArray.length() ; i++) {
                JSONObject another_json_object = (JSONObject) jsonArray.get(i);

                JSONArray jsonArray1 = another_json_object.getJSONArray("ID");
                for (int j = 0 ; j < jsonArray1.length() ; j++) {
                    JSONArray jsonArray11 = (JSONArray) jsonArray1.get(j);

                    for (int j1 = 0 ; j1 < jsonArray11.length() ; j1++) {
                        System.out.println(jsonArray11.get(j1));
                    }
                }

                JSONArray jsonArray2 = another_json_object.getJSONArray("Name");
                for (int k = 0 ; k < jsonArray2.length() ; k++) {
                    JSONArray jsonArray21 = (JSONArray) jsonArray2.get(k);
                    for (int k1 = 0 ; k1 < jsonArray21.length() ; k1++) {
                        System.out.println(jsonArray21.get(k1));
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}

Output=>

=>{"tag":"Login","tdata":[{"ID":[["C1","C2"]],"Name":[["ABC","PQR"]]}]}
C1
C2
ABC
PQR

Comments

0

All you need is to understand the JsonObject Api of java a little better : http://examples.javacodegeeks.com/core-java/json/java-json-parser-example/

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.