简体   繁体   中英

Java Parse JSON Multidimensional Object element to String

I have JSONObject data with multiple object inside. What i want to do is, make that json to simple hierarchy.

JSON Data

{
    "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"
                            }
                        }
                    ]
                }
            }
        ]
    }
}

My code

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

with my code, i get this result :

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

only work for field with no multiple elements. but what i wanted result is :

{
    "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
                        }
                    ]
                }
            }
        ]
    }
}

i have no idea how to remove objects from Data field.

Here is a program that creates the output in the format you want: like you, I have used recursion, the changes I made were:

  • I handled arrays
  • I created a new object to collect the data for a child that does not have the 'content' key.

Here is the code:

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

    }
}

Here is its output:

{
  "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"
        }
      ]}
    }
  ]}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM