简体   繁体   中英

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;
        JSONObject jso3 = new JSONObject(JSON);
        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);    
    }

I created the JSON within the MainActivity.java, it's not on a separate file.

Here is the code of the JSON creation:

private void create_JSON()
{
    JSONObject jso = new JSONObject();

    try {
        jso.put("Nombre","Miguel");
        jso.put("Apellidos", "Garcia");
        jso.put("Año_nacimiento", 1990);
        JSONArray jsa = new JSONArray();
        jsa.put("Blur");
        jsa.put("Clur");
        jso.put("Nombres_Hijos", jsa);

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    jso.toString();

I have no doubts that the JSON is correctly created, I just need help in understanding how do I parse it and convert it into a String.

I would be very grateful if you could point out to me the flaws in my programming.

Mauro.

Try this..

Your response like below

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

To parse the JSON use below code:

JSONObject jso3 = new JSONObject(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));
}

Use the following options while parsing JSON to avoid common error.

JSONObject jso3 = new JSONObject(output);
String name = jso3.optString("Nombre",""); // here default value is blank ("")
String surname = jso3.optString("Apellidos",null);// here default value is null
int date = jso3.getInt("Año_nacimiento",0); // here default value is ZERO (0)
JSONArray menuObject = jso3.getJSONArray("Nombres_Hijos");
for(int i=0;i<menuObject.length;i++){   
System.out.println(menuObject.getString(i));
}

Using option you can set default return value. 选项,您可以设置默认返回值。 Event if that tag not available in JSON data you will get default value.

This works for me better than GSON lib.

try{   

 String JSON ;
        JSONObject jso3 = new JSONObject(JSON);
        JSONArray menuObject = new JSONArray(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

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

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:

 String child_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"]
private void read_JSON(String json)
    {

       JSONObject jObject= new JSONObject(json);
        JSONArray jso3= new JSONArray(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);    
    }

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