Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java android中使用截取的嵌套JSON对象解析_Java_Android_Json - Fatal编程技术网

Java android中使用截取的嵌套JSON对象解析

Java android中使用截取的嵌套JSON对象解析,java,android,json,Java,Android,Json,我需要您帮助解析嵌套的JSON对象 附加JSON数据: { "31": { "basic": { "node_id": "31", "title": "test", "alias": "test", "description": "test", "site_id": "151336557", "node_type": "7", "privacy": "7", "deleted": "0",

我需要您帮助解析嵌套的JSON对象

附加JSON数据:

{
  "31": {
    "basic": {
      "node_id": "31",
      "title": "test",
      "alias": "test",
      "description": "test",
      "site_id": "151336557",
      "node_type": "7",
      "privacy": "7",
      "deleted": "0",
      "status": "1",
      "created_date": "1379169518",
      "updated_date": "1379169518",
      "created_by": "140513626519686828",
      "updated_by": null,
      "readable_date": "14th Sep, 2013"
    },
    "meta": {
      "forum_id": "61"
    },
    "comments": {
      "count": 1
    },
    "likes": {
      "count": 0
    },
    "tags": [],
    "node_id": "31"
  },
  "32": {
    "basic": {
      "node_id": "32",
      "title": "testing discussion",
      "alias": "testing-discussion",
      "description": "testing",
      "site_id": "151336557",
      "node_type": "7",
      "privacy": "7",
      "deleted": "0",
      "status": "1",
      "created_date": "1379493816",
      "updated_date": "1379493816",
      "created_by": "140513795022034166",
      "updated_by": null,
      "readable_date": "18th Sep, 2013"
    },
    "meta": {
      "forum_id": "65"
    },
    "comments": {
      "count": 1
    },
    "likes": {
      "count": 0
    },
    "tags": [],
    "node_id": "32"
  }
}
附加Java代码:

private void makeJsonObjectRequest() {
     showpDialog();

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

         @Override
         public void onResponse(JSONObject response) {
             Log.d(TAG, response.toString());

             JSONObject phone = response.getJSONObject("31").getJSONObject("basic");
             String name = phone.getString("title");
             String email = phone.getString("description");
             JSONObject comments = response.getJSONObject("31").getJSONObject("comments");
             String home = comments.getString("count");
             JSONObject like = response.getJSONObject("31").getJSONObject("likes");
             String mobile = like.getString("count");

             jsonResponse = "";
             jsonResponse += "Name: " + name + "\n\n";
             jsonResponse += "Email: " + email + "\n\n";
             jsonResponse += "Home: " + home + "\n\n";
             jsonResponse += "Mobile: " + mobile + "\n\n\n";

             txtResponse.setText(jsonResponse);
private void makeJsonObjectRequest(){
showpDialog();
JsonObjectRequest JSONObjectReq=新的JsonObjectRequest(Method.GET,urlJsonObj,null,new Response.Listener()){
@凌驾
公共void onResponse(JSONObject响应){
Log.d(TAG,response.toString());
JSONObject phone=response.getJSONObject(“31”).getJSONObject(“基本”);
字符串名称=phone.getString(“title”);
String email=phone.getString(“说明”);
JSONObject comments=response.getJSONObject(“31”).getJSONObject(“comments”);
String home=comments.getString(“count”);
JSONObject like=response.getJSONObject(“31”).getJSONObject(“likes”);
String mobile=like.getString(“count”);
jsonResponse=“”;
jsonResponse+=“名称:”+Name+“\n\n”;
jsonResponse+=“电子邮件:”+Email+“\n\n”;
jsonResponse+=“Home:“+Home+”\n\n”;
jsonResponse+=“Mobile:“+Mobile+”\n\n\n”;
setText(jsonResponse);
我需要检索所有对象,但这里我只检索一个对象(我指的是所有
node\u id
s)。我需要您的建议。 谢谢。

关于这一部分

response.getJSONObject("31")

您需要更改变量的
31
,并将其调用到对象的每个字段

您将获得带有键的条目
“31”
response.getJSONObject(“31”)
),但是,您应该迭代所有键:

 JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET, urlJsonObj, null, new Response.Listener<JSONObject>() {
     @Override
     public void onResponse(JSONObject response) {
         for (String key : response.keySet()) {
             JSONObject entry = response.getJSONObject(key);
             Log.d(TAG, entry.toString());

             JSONObject phone = entry.getJSONObject("basic");
             String name = phone.getString("title");
             String email = phone.getString("description");
             JSONObject comments = entry.getJSONObject("comments");
             String home = comments.getString("count");
             JSONObject like = entry.getJSONObject("likes");
             String mobile = like.getString("count");

             jsonResponse = "";
             jsonResponse += "Name: " + name + "\n\n";
             jsonResponse += "Email: " + email + "\n\n";
             jsonResponse += "Home: " + home + "\n\n";
             jsonResponse += "Mobile: " + mobile + "\n\n\n";

             txtResponse.setText(txtResponse.getText() + "\n\n" + jsonResponse); //get the old text and add it to it
         }
     } 
}

使用此网站,它可以帮助您从Json转换为Objects感谢您的回复。我尝试了您的代码,但我发现response.keys()的错误“只能迭代数组或java.lang.Iterable的实例”。您可以建议吗?@srikanth Make
keys()
keySet()
@gogu您从哪个包导入JSONObject?
 JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET, urlJsonObj, null, new Response.Listener<JSONObject>() {
     @Override
     public void onResponse(JSONObject response) {
         List<String> nodeIds = new ArrayList<String>();
         for (String key : response.keySet()) {
             JSONObject entry = response.getJSONObject(key);
             nodeIds.add(enty.getJSONObject("basic").getString("node_id"));
         }
         txtResponse.setText(ListUtils.toString(nodeIds)); //from apaches commons library
     }
}