简体   繁体   中英

JSON data parsing to Android App

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"]]

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

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();
    }

您需要的是更好地理解Java的JsonObject Api: http : //examples.javacodegeeks.com/core-java/json/java-json-parser-example/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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