У меня есть данные JSONObject с несколькими объектами внутри. Что я хочу сделать, сделайте этот json простой иерархией.Java Parse 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": {
"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"
}
}
]
}
}
]
}
}
работать только поле без каких-либо нескольких элементов. , но то, что я хотел результат является:
{
"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
}
]
}
}
]
}
}
я понятия не имею, как удалить объекты из поля данных.
Это будет лучше и чище, если вы можете тайный JSON в объект Java и запускать операции на том, что вместо если еще петли. –
Я не использовал JSON много в Java, но в JavaScript они предоставляют методы строкового и синтаксического анализа для преобразования объектов в JSON и обратно. Я уверен, что Java имеет нечто подобное. –
Можете ли вы также показать мне, где объявлен objResponse? У меня такое ощущение, что объект может быть виновником. –