简体   繁体   English

Android JSON解析和转换

[英]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. 我正在尝试将JSON data解析为字符串,但是我认为我做错了什么:这是本节。

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. 我创建了JSON的MainActivity.java ,它不是一个单独的文件。

Here is the code of the JSON creation: 这是JSON创建的代码:

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. 我毫不怀疑正确创建了JSON ,我只需要了解如何解析并将其转换为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: 解析 JSON使用以下代码:

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. 解析JSON时,请使用以下选项,以避免常见错误。

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 opt option you can set default return value. 使用opt选项,您可以设置默认返回值。 Event if that tag not available in JSON data you will get default value. 如果该标签在JSON数据中不可用,则会发生事件,您将获得默认值。

This works for me better than GSON lib. 这对我来说比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. 首先,您似乎忽略了在read_JSON中创建的字符串,但是我认为您这样做是为了避免在此处粘贴过多的代码。

Problem is this line: 问题是这一行:

 String child_names = jso3.getString("Nombres_Hijos");

Because fields Nombres_Hijos is JsonArray, not String. 因为Nombres_Hijos字段是JsonArray,而不是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);    
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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