2017-08-03 79 views
-2

我试图解析的多个对象的JSON对象,娄我收到的Json样品如何解析JSON对象的客体

{ 
    "0": //outer objects are multiples, i just post one object for sample 
    { 
     "id": "1",  
     "name": "B2 MR1", 
     "description": 
     { 
      "0": //it is also multiple objects m just showing one 
      { 

       "title": "Carve the Future", 
       "description": "Welcome to Meeting Room 1", 
       "push_notification": "Carve the Future", 

      } 
     } 
    },//after that the next obj will be show 
    . 
    . 
} 

在第二对象我也有上述键和价值观,我无法处理它,这是我的模型。

public class JsonModel { 

     private String id; //getter setter 
     private String name; //getter setter 
    List<InnerDescprtion> description; //getter setter 
    } 

这里是我的InnerDescprtion型号

private class InnerDescprtion { 
     private String id; //getter setter 
     private String title; //getter setter 
    } 

而且下面是我的Java代码使用GSON解析它,

JsonModel outterModelClass= null; 
        List<JsonModel> listObj = new ArrayList<>(); 
        for (int i = 0; i < responseJson.length(); i++) { 

         try { 
          outterModelClass= new Gson().fromJson(responseJson.getString(String.valueOf(i)), JsonModel.class); 
          listObj.add(outterModelClass); //here i'm getting exception,, 
         } catch (JSONException e) { 
          e.printStackTrace(); 
          } 
        } 

我没有得到解决,需要解决方案谢谢

+0

你得到了什么类型的异常? – Ivied

回答

0

如果有可能你,我会json的改变是这样的:

[{ 
     "id": "1", 
     "name": "B2 MR1", 
     "description": [{ 
      "id" : "1-1", 
      "title": "Carve the Future", 
      "description": "Welcome to Meeting Room 1", 
      "push_notification": "Carve the Future" 
     }] 
    }, 
    { 
     "id": "2", 
     "name": "B2 MR2", 
     "description": [{ 
      "id" : "2-1", 
      "title": "Carve the Future 2", 
      "description": "Welcome to Meeting Room 2", 
      "push_notification": "Carve the Future 2" 
     }] 
    } 
] 

然后你的方法应该只有几个变化工作:

BufferedReader br = new BufferedReader(new FileReader("c:/test/test.json")); 
Type listType = new TypeToken<ArrayList<JsonModel>>(){}.getType(); 

List<JsonModel> outterModels = new Gson().fromJson(br, listType); 

如果你不能改变JSON我建议使用另一个像JSON简单的JSON库,并手动提取所有内容。

0

你 'listObj' 应该这样定义的:

ArrayList<JsonModel> listObj = new ArrayList<JsonModel>(); 
0

那么这是一个讨厌的JSON。不过,我建议你使用凌空android库。我有一个有点类似问题的任务。只有另一个对象内部存在单个对象。要在您的项目中包含排球,请在dependencies{}内更新您的build.gradleapp模块compile 'com.android.volley:volley:1.0.0'baseUrl是您从中获取JSON的网址。

然后,你可以这样做:

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, 
      baseUrl, 
      null, 
      new Response.Listener<JSONObject>() { 

       @Override 
       public void onResponse(JSONObject response) { 

        try { 
         // Parsing json object response 
         // response will be a json object 
         for (int i=0; i<response.length(); i++){ 
          JSONObject obj = response.getJSONObject(i); 

          //id 
          //name 
          try{ 
           for (int j=0; j<obj.length() ; j++) { 
           JSONObject description = obj.getJSONObject(j); 
           //title 
           //description 
           //push notification 

           } 
          } catch (JSONException e) { 
           e.printStackTrace(); 
           Toast.makeText(getApplicationContext(), 
           "Error: " + e.getMessage(), 
           Toast.LENGTH_LONG).show(); 
          } 


         } 

        } catch (JSONException e) { 
         e.printStackTrace(); 
         Toast.makeText(getApplicationContext(), 
           "Error: " + e.getMessage(), 
           Toast.LENGTH_LONG).show(); 
        } 

        hidepDialog(); 

       } 

      }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError volleyError) { 
      VolleyLog.d(TAG,"Error: "+ volleyError.getMessage()); 
      Toast.makeText(getApplicationContext(), volleyError.getMessage(), Toast.LENGTH_SHORT).show(); 
      hidepDialog(); 
     } 
    }); 
    //adding request to request queue 
    AppController.getmInstance().addToRequestQueue(jsonObjReq); 

parseJSON(){}方法或任何你把它命名为添加此。

我还没有尝试过你在做什么。但似乎可行,使用排球库。