Skip to content Skip to sidebar Skip to footer

Android Json Parsing And Conversion

Good morning. I am trying to parse JSON data into a string but I think I'm doing something wrong: here is the section. private void read_JSON() { String JSON; JSONO

Solution 1:

Try this..

Your response like below

{                             ==> JSONObject"Año_nacimiento": 1990,   ==> StringfromJSONObject"Nombres_Hijos": [        ==> JSONArray"Blur",               ==> DirectlyfromJSONArray"Clur"
    ],
    "Apellidos": "Garcia",
    "Nombre": "Miguel"
}

To parse the JSON use below code:

JSONObject jso3 = newJSONObject(output);
String name = jso3.getString("Nombre");
String surname = jso3.getString("Apellidos");
int date = jso3.getInt("Año_nacimiento");
JSONArray menuObject = jso3.getJSONArray("Nombres_Hijos");
for(int i=0;i<menuObject.length;i++){   
   System.out.println(menuObject.getString(i));
}

Solution 2:

Try this.

String apellidos = jso.getString("Apellidos");
System.out.println(apellidos);

int str2 = jso.getInt("Año_nacimiento");
System.out.println(str2);

String nombre = jso.getString("Nombre");
System.out.println(nombre);

JSONArray array = jso.getJSONArray("Nombres_Hijos");
for(int i = 0; i < array.length(); i++){
    System.out.println(array.get(i));
}

Solution 3:

First at all, you seem to ignore Strings created in read_JSON, but i assume you do this to avoid pasting here too much code.

Problem is this line:

Stringchild_names= jso3.getString("Nombres_Hijos");

Because fields Nombres_Hijos is JsonArray, not String. To read it use:

JSONArray jsa = jso3.getJSONArray("Nombres_Hijos");

Now all depands what you need to do later with this data. Easiest case would be:

String names = jsa.toString(); //["Blur","Clur"]

Solution 4:

privatevoidread_JSON(String json)
    {

       JSONObject jObject= newJSONObject(json);
        JSONArray jso3= newJSONArray(jObject.getString("Nombres_Hijos"));   


        for (int i=0; i < jso3.length(); i++)
        {

        try
        {

            String name = jso3.getString("Nombre");
            String surname = jso3.getString("Apellidos");
            String date = jso3.getString("Año_nacimiento");
            String child_names = jso3.getString("Nombres_Hijos");


        }catch (JSONException e)
        {
            e.printStackTrace();
        }

        }
    jso3.toString(JSON);    
    }

Solution 5:

try{   

 StringJSON ;
        JSONObject jso3 = newJSONObject(JSON);
        JSONArray menuObject = newJSONArray(jObject.getString("array_inside_json"));   
        for(int i=0;i<menuObject.length;i++){   
        name=jObject.getString("inside"));          


        }

}catch(Exception e){

}

refer this link for more info

Post a Comment for "Android Json Parsing And Conversion"