Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.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将JSON多维对象元素解析为字符串_Java_Arrays_Json - Fatal编程技术网

Java将JSON多维对象元素解析为字符串

Java将JSON多维对象元素解析为字符串,java,arrays,json,Java,Arrays,Json,我有JSONObject数据,里面有多个对象。 我想做的是,将json转换为简单的层次结构 JSON数据 { "Response": { "type": "string", "content": "0000" }, "Data": { "item": [ { "firstname": { "type": "string",

我有JSONObject数据,里面有多个对象。 我想做的是,将json转换为简单的层次结构

JSON数据

{
    "Response": {
        "type": "string",
        "content": "0000"
    },
    "Data": {
        "item": [
            {
                "firstname": {
                    "type": "string",
                    "content": "Bryan"
                },
                "lastname": {
                    "type": "string",
                    "content": "Adams"
                },
                "kids": {
                    "item": [
                        {
                            "name": {
                                "type": "string",
                                "content": "Tommy"
                            },
                            "age": {
                                "type": "string",
                                "content": "9"
                            }
                        },
                        {
                            "name": {
                                "type": "string",
                                "content": "Jane"
                            },
                            "age": {
                                "type": "string",
                                "content": "4"
                            }
                        }
                    ]
                }
            },
            {
                "firstname": {
                    "type": "string",
                    "content": "Joey"
                },
                "lastname": {
                    "type": "string",
                    "content": "Cena"
                },
                "kids": {
                    "item": [
                        {
                            "name": {
                                "type": "string",
                                "content": "Maria"
                            },
                            "age": {
                                "type": "string",
                                "content": "7"
                            }
                        },
                        {
                            "name": {
                                "type": "string",
                                "content": "Dany"
                            },
                            "age": {
                                "type": "string",
                                "content": "3"
                            }
                        }
                    ]
                }
            }
        ]
    }
}
我的代码

package junk;

import java.util.Iterator;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author Agung
 */
class Foo {

    private static JSONObject objResponse = new JSONObject();

    public static void main(String args[]) throws JSONException {
        JSONObject jsonObj = new JSONObject("{\"Response\":{\"type\":\"string\",\"content\":\"0000\"},\"Data\":{\"item\":[{\"firstname\":{\"type\":\"string\",\"content\":\"Bryan\"},\"lastname\":{\"type\":\"string\",\"content\":\"Adams\"},\"kids\":{\"item\":[{\"name\":{\"type\":\"string\",\"content\":\"Tommy\"},\"age\":{\"type\":\"string\",\"content\":\"9\"}},{\"name\":{\"type\":\"string\",\"content\":\"Jane\"},\"age\":{\"type\":\"string\",\"content\":\"4\"}}]}},{\"firstname\":{\"type\":\"string\",\"content\":\"Joey\"},\"lastname\":{\"type\":\"string\",\"content\":\"Cena\"},\"kids\":{\"item\":[{\"name\":{\"type\":\"string\",\"content\":\"Maria\"},\"age\":{\"type\":\"string\",\"content\":\"7\"}},{\"name\":{\"type\":\"string\",\"content\":\"Dany\"},\"age\":{\"type\":\"string\",\"content\":\"3\"}}]}}]}}");
        Foo.getResponseContent(jsonObj);
        System.out.println(objResponse);
    }

    private static void getResponseContent(JSONObject jsonObj) throws JSONException {
        Iterator<?> keys = jsonObj.keys();
        while (keys.hasNext()) {
            String key = (String) keys.next();
            if (jsonObj.get(key) instanceof JSONObject) {
                JSONObject object = jsonObj.getJSONObject(key);
                if (object.has("content")) {
                    String content = (String) object.get("content");
                    objResponse.put(key, content);
                } else {
                    // if we get here, so the element have multiple node
                    objResponse.put(key, object);
                    getResponseContent(object);
                }
            }
        }
    }
}
仅适用于没有多个元素的字段。 但我想要的结果是:

{
    "Response": "0000",
    "Data": {
        "item": [
            {
                "firstname": "Bryan",
                "lastname": "Adams",
                "kids": {
                    "item": [
                        {
                            "name": "Tommy",
                            "age": 9
                        },
                        {
                            "name": "Jane",
                            "age": 4
                        }
                    ]
                }
            },
            {
                "firstname": "Joey",
                "lastname": "Cena",
                "kids": {
                    "item": [
                        {
                            "name": "Maria",
                            "age": 7
                        },
                        {
                            "name": "Dany",
                            "age": 3
                        }
                    ]
                }
            }
        ]
    }
}

我不知道如何从数据字段中删除对象。

这里有一个程序,它以您想要的格式创建输出:像您一样,我使用了递归,我所做的更改是:

  • 我处理数组
  • 我创建了一个新对象来收集没有“content”键的子对象的数据
代码如下:

package test;

import java.util.Iterator;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class TestMain {

    public static void main(String args[]) throws Exception {
        JSONObject objResponse = new JSONObject();
        JSONObject jsonObj = new JSONObject(
                "{\"Response\":{\"type\":\"string\",\"content\":\"0000\"},\"Data\":{\"item\":[{\"firstname\":{\"type\":\"string\",\"content\":\"Bryan\"},\"lastname\":{\"type\":\"string\",\"content\":\"Adams\"},\"kids\":{\"item\":[{\"name\":{\"type\":\"string\",\"content\":\"Tommy\"},\"age\":{\"type\":\"string\",\"content\":\"9\"}},{\"name\":{\"type\":\"string\",\"content\":\"Jane\"},\"age\":{\"type\":\"string\",\"content\":\"4\"}}]}},{\"firstname\":{\"type\":\"string\",\"content\":\"Joey\"},\"lastname\":{\"type\":\"string\",\"content\":\"Cena\"},\"kids\":{\"item\":[{\"name\":{\"type\":\"string\",\"content\":\"Maria\"},\"age\":{\"type\":\"string\",\"content\":\"7\"}},{\"name\":{\"type\":\"string\",\"content\":\"Dany\"},\"age\":{\"type\":\"string\",\"content\":\"3\"}}]}}]}}");
        getResponseContent(jsonObj, objResponse);
        System.out.println(objResponse.toString(2));
    }

    private static void getResponseContent(JSONObject jsonObj, JSONObject objResponse) throws JSONException {

        Iterator<?> keys = jsonObj.keys();
        while (keys.hasNext()) {
            String key = (String) keys.next();
            JSONObject child = jsonObj.optJSONObject(key);
            if (child != null) {
                if (child.has("content")) {
                    objResponse.put(key, child.get("content"));
                } else {
                    JSONObject responseChild = new JSONObject();
                    objResponse.put(key, responseChild);
                    getResponseContent(child, responseChild);
                }
            } else {
                JSONArray children = jsonObj.optJSONArray(key);
                if (children != null) {
                    JSONArray responseChildren = new JSONArray();
                    objResponse.put(key, responseChildren);
                    for (int i = 0; i < children.length(); i++) {
                        child = children.getJSONObject(i);
                        JSONObject responseChild = new JSONObject();
                        responseChildren.put(responseChild);
                        getResponseContent(child, responseChild);
                    }
                }
            }
        }

    }
}

如果您可以将json转换为java对象,并在该对象上运行操作,而不是if-else循环,则会更好、更干净。我在java中很少使用json,但在JavaScript中,它们提供了将对象转换为json并返回的字符串化和解析方法。我相信Java也有类似的东西。你能告诉我objResponse是在哪里声明的吗?我有一种感觉,物体可能是罪魁祸首。
package test;

import java.util.Iterator;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class TestMain {

    public static void main(String args[]) throws Exception {
        JSONObject objResponse = new JSONObject();
        JSONObject jsonObj = new JSONObject(
                "{\"Response\":{\"type\":\"string\",\"content\":\"0000\"},\"Data\":{\"item\":[{\"firstname\":{\"type\":\"string\",\"content\":\"Bryan\"},\"lastname\":{\"type\":\"string\",\"content\":\"Adams\"},\"kids\":{\"item\":[{\"name\":{\"type\":\"string\",\"content\":\"Tommy\"},\"age\":{\"type\":\"string\",\"content\":\"9\"}},{\"name\":{\"type\":\"string\",\"content\":\"Jane\"},\"age\":{\"type\":\"string\",\"content\":\"4\"}}]}},{\"firstname\":{\"type\":\"string\",\"content\":\"Joey\"},\"lastname\":{\"type\":\"string\",\"content\":\"Cena\"},\"kids\":{\"item\":[{\"name\":{\"type\":\"string\",\"content\":\"Maria\"},\"age\":{\"type\":\"string\",\"content\":\"7\"}},{\"name\":{\"type\":\"string\",\"content\":\"Dany\"},\"age\":{\"type\":\"string\",\"content\":\"3\"}}]}}]}}");
        getResponseContent(jsonObj, objResponse);
        System.out.println(objResponse.toString(2));
    }

    private static void getResponseContent(JSONObject jsonObj, JSONObject objResponse) throws JSONException {

        Iterator<?> keys = jsonObj.keys();
        while (keys.hasNext()) {
            String key = (String) keys.next();
            JSONObject child = jsonObj.optJSONObject(key);
            if (child != null) {
                if (child.has("content")) {
                    objResponse.put(key, child.get("content"));
                } else {
                    JSONObject responseChild = new JSONObject();
                    objResponse.put(key, responseChild);
                    getResponseContent(child, responseChild);
                }
            } else {
                JSONArray children = jsonObj.optJSONArray(key);
                if (children != null) {
                    JSONArray responseChildren = new JSONArray();
                    objResponse.put(key, responseChildren);
                    for (int i = 0; i < children.length(); i++) {
                        child = children.getJSONObject(i);
                        JSONObject responseChild = new JSONObject();
                        responseChildren.put(responseChild);
                        getResponseContent(child, responseChild);
                    }
                }
            }
        }

    }
}
{
  "Response": "0000",
  "Data": {"item": [
    {
      "firstname": "Bryan",
      "lastname": "Adams",
      "kids": {"item": [
        {
          "name": "Tommy",
          "age": "9"
        },
        {
          "name": "Jane",
          "age": "4"
        }
      ]}
    },
    {
      "firstname": "Joey",
      "lastname": "Cena",
      "kids": {"item": [
        {
          "name": "Maria",
          "age": "7"
        },
        {
          "name": "Dany",
          "age": "3"
        }
      ]}
    }
  ]}
}