MM Manuel
MM Manuel

Reputation: 385

How to Parse JSON which contains the same key names

I have the following JSON:

 [
 {"hex":"3443d3", "squawk":"4673", "flight":"VLG3014 ","lat":37.716344, "lon":-2.913646, "validposition":1, "altitude":35000,"vert_rate":0,"track":225, "validtrack":1,"speed":427, "messages":74, "seen":0},
 {"hex":"345313", "squawk":"3767", "flight":"VLG2422 ", "lat":37.573568, "lon":-2.912750, "validposition":1, "altitude":38000,  "vert_rate":0,"track":72, "validtrack":1,"speed":420, "messages":1217, "seen":0}
 ]

The code is the following:

@Override
protected String doInBackground(String... strings) {

    try {
        socket=new Socket("172.24.1.1",9000);
        System.out.println("CONECTED");
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        System.out.println("BufferedReader READY");
    } catch (IOException e) {
        e.printStackTrace();
    }

    int i=0;
    while(i==0) {
        try {
            System.out.println("WAITING FOR MESSAGE");
            inputString=br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            if(inputString!=null) {
                jsonString = new JSONObject(inputString);
                System.out.println(parseJSON(jsonString)[0]);
            }
        } catch (JSONException e) {
            //e.printStackTrace();
        }

    }

    return null;
}

private String[] parseJSON(JSONObject jsonObject) throws JSONException {
    String hex = jsonObject.getString("hex");
    String squawk = jsonObject.getString("squawk");
    String flight = jsonObject.getString("flight");
    String lat = jsonObject.getString("lat");
    String lon = jsonObject.getString("lon");
    String validposition = jsonObject.getString("validposition");
    String altitude = jsonObject.getString("altitude");
    String vert_rate = jsonObject.getString("vert_rate");
    String track= jsonObject.getString("track");
    String validtrack = jsonObject.getString("validtrack");
    String speed = jsonObject.getString("speed");
    String messages = jsonObject.getString("messages");
    String seen = jsonObject.getString("seen");

    String[] answer={hex,squawk,flight,lat,lon,validposition,altitude,vert_rate,track,validtrack,speed,messages,seen};
    return answer;
}

I receive the entire JSON in my app. When I get "hex", I obtain 345313 code. I want to have a vector of "hex" with both values, my question is: How can I obtain both values without using for loop? Is there any JSON method to obtain both at the same time and save them in an array?

Upvotes: 0

Views: 1125

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191725

You have a JSONArray according to your question, not a JSONObject

If you want no loops, you can get both JsonObject elements by indexing the array using getJSONObject(0) and getJSONObject(1). Obviously a loop is better.

Then just use getString("hex") for both objects like you already did to get the same key for both.

Upvotes: 1

Related Questions