【问题标题】:How to Parse Json Objects of Json Object and of Object (outdated) [duplicate]如何解析 Json 对象和对象的 Json 对象(过时)[重复]
【发布时间】:2017-08-03 13:12:01
【问题描述】:

我正在尝试解析多个对象,我正在接收 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
  .
  .
}

在第二个对象1我也有上面的键和值,我不能处理它,这是我的模型。

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

我得到了解决方案,请投票帮助我。

【问题讨论】:

  • 你得到什么类型的异常?

标签: java android json gson


【解决方案1】:

如果可以的话,我会把 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 simple 并手动提取所有内容。

【讨论】:

    【解决方案2】:

    您的“listObj”应该这样定义:

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

    【讨论】:

      【解决方案3】:

      嗯,这是一个令人讨厌的 JSON。但是我建议你使用 volley android 库。我有一个类似问题的任务。另一个对象内部只有一个对象。要将 volley 包含在您的项目中,请在 dependencies{} 中使用 compile 'com.android.volley:volley:1.0.0' 更新您的 build.gradle app 模块。 baseUrl 是您从中获取 JSON 的 url。

      然后你可以这样做:

      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(){} 方法或您命名的任何内容中。

      我没有尝试做你想做的事。但这似乎是可行的,使用 volley 库。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-05-30
        • 2021-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-28
        • 2017-09-24
        相关资源
        最近更新 更多