Parsing JSON array and object in Android

前端 未结 6 1919
渐次进展
渐次进展 2021-01-07 15:22

This is what the JSON looks like:

[{
    \"pmid\": \"2\",
    \"name\": \" MANAGEMENT\",
    \"result\": \"1\",
    \"properties\": [
        {
            \         


        
6条回答
  •  失恋的感觉
    2021-01-07 15:42

    Here is complete example with resolution.

    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    
    
    public class Test {
    
    public static void main(String[] args) 
    {
         JSONObject jObj = null;
        try {
            String jsonStr = "[{\"pmid\":\"2\",\"name\":\" MANAGEMENT\",\"result\":\"1\",\"properties\":[{\"prop_id\":\"32\",\"prop_name\":\"Bonneville\",\"address\":\"122 Lakeshore\",\"city\":\"Ripley\",\"state\":\"OH\",\"zip\":\"11454\",\"lat\":\"41.123\",\"long\":\"-85.5034\"}]}]";
            jsonStr = jsonStr.substring(1, jsonStr.length()-1);
              System.out.println(jsonStr);
            jObj = new JSONObject(jsonStr);
    
    
    
            System.out.println("pmid="+jObj.get("pmid"));
            System.out.println("name="+jObj.get("name"));
            System.out.println("result="+jObj.get("result"));
    
    
            JSONArray jArr = jObj.getJSONArray("properties");
    
            JSONObject c = jArr.getJSONObject(0);
    
            System.out.println("prop_id=="+c.get("prop_id"));
            System.out.println("prop_name=="+c.get("prop_name"));
            System.out.println("address=="+c.get("address"));
            System.out.println("city=="+c.get("city"));
            System.out.println("state=="+c.get("state"));
            System.out.println("zip=="+c.get("zip"));
            System.out.println("lat=="+c.get("lat"));
            System.out.println("long=="+c.get("long"));
    
    
        } catch (JSONException e) 
        {
            e.printStackTrace();
        }
    }
    
    }
    

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题