2014-03-01 2 views
0

아래와 같이 재귀 적으로 알려지지 않은 json 입력 구조를 구문 분석하려고합니다. 다른 json에서 같은 구조를 다시 작성하려고합니다.자바에서 재귀 적으로 알 수없는 json 입력 구조를 구문 분석합니다.

한편 나는 구문 분석하는 동안 각각의 json 키/값을 &마다 확인해야합니다.

{"Verbs":[{ 
    "aaaa":"30d", "type":"ed", "rel":1.0, "id":"80", "spoken":"en", "ct":"on", "sps":null 
},{ 
    "aaaa":"31", "type":"cc", "rel":3.0, "id":"10", "spoken":"en", "ct":"off", "sps":null 
},{ 
    "aaaa":"81", "type":"nn", "rel":3.0, "id":"60", "spoken":"en", "ct":"on", "sps":null 
}]} 

알 수없는 json 콘텐츠를 읽고 쓰는 데 사용할 수있는 json 파서에 대한 조언을 구하십시오.

+2

모두. 그것의 json; 그들은 그것을 분석한다. –

+0

각 json 요소를 구문 분석하고 입력과 동일한 방식으로 다시 어셈블하는 방법 – user3218475

+0

파싱 된 데이터로 수행 할 작업은 무엇입니까? –

답변

3

재귀 JSON 개체를 구문 분석 할 수있는이 방법 :

import com.eclipsesource.json.JsonArray; 
import com.eclipsesource.json.JsonObject; 
import com.eclipsesource.json.JsonValue; 

public class JsonQuestion { 

    public static void main(String[] args) { 
     String input = "{\"Verbs\":[{\n" + 
       " \"aaaa\":\"30d\", \"type\":\"ed\", \"rel\":1.0, \"id\":\"80\", \"spoken\":\"en\", \"ct\":\"on\", \"sps\":null\n" + 
       "},{\n" + 
       " \"aaaa\":\"31\", \"type\":\"cc\", \"rel\":3.0, \"id\":\"10\", \"spoken\":\"en\", \"ct\":\"off\", \"sps\":null\n" + 
       "},{\n" + 
       " \"aaaa\":\"81\", \"type\":\"nn\", \"rel\":3.0, \"id\":\"60\", \"spoken\":\"en\", \"ct\":\"on\", \"sps\":null\n" + 
       "}]}"; 

     JsonObject jsonObject = JsonObject.readFrom(input); 
     handleObject(jsonObject); 
    } 

    private static void handleValue(JsonObject.Member member, JsonValue value) { 
     if (value.isArray()) { 
      if (member != null) { 
       System.out.print("name = " + member.getName()); 
      } 
      System.out.println("array value "); 
      recurseArray(value.asArray()); 
     } else if (value.isBoolean()) { 
      if (member != null) { 
       System.out.print("name = " + member.getName()); 
      } 
      System.out.println(", boolean value = " + value.asBoolean()); 
     } else if (value.isNull()) { 
      if (member != null) { 
       System.out.print("name = " + member.getName()); 
      } 
      System.out.println(", null value"); 
     } else if (value.isNumber()) { 
      if (member != null) { 
       System.out.print("name = " + member.getName()); 
      } 
      System.out.println(", number value = " + value.asDouble()); 
     } else if (value.isObject()) { 
      if (member != null) { 
       System.out.print("name = " + member.getName()); 
      } 
      System.out.println(", object value "); 
      handleObject(value.asObject()); 
     } else if (value.isString()) { 
      if (member != null) { 
       System.out.print("name = " + member.getName()); 
      } 
      System.out.println(", string value = " + value.asString()); 
     } 
    } 

    private static void handleObject(JsonObject object) { 
     for (JsonObject.Member next : object) { 
      JsonValue value = next.getValue(); 
      handleValue(next, value); 
     } 
    } 

    private static void recurseArray(JsonArray array) { 
     for (JsonValue value : array) { 
      handleValue(null, value); 
     } 
    } 
} 
+0

Thanks 귀하의 회신에 대해, Parallely 우리는 각각의 모든 값을 구문 분석하는 동안 json 응답 프레임 수 있습니다. – user3218475

+0

죄송합니다. 귀하의 의견을 이해할 수 없습니까? 질문입니까? –

+0

예. gson/jackson 라이브러리에서 동일한 구문 분석 논리를 얻을 수 있습니까? 우리 프로젝트에서만 gson/jackson 라이브러리를 사용하기 때문에 – user3218475

-2

는이 방법으로, 예를 들어, 그것을 할 수 Jackson 라이브러리를 사용하여 :

ObjectMapper mapper = new ObjectMapper(); 
JsonNode rootNode = mapper.readTree(json); 
ArrayNode verbs = (ArrayNode) rootNode.get("Verbs"); 
int size = verbs.size(); 
for (int index = 0; index < size; index++) { 
    JsonNode itemNode = verbs.get(index); 
    System.out.println(itemNode); 
    System.out.println(itemNode.get("aaaa").asText()); 
    System.out.println(itemNode.get("type").asText()); 
    System.out.println(itemNode.get("rel").asInt()); 
    System.out.println(itemNode.get("id").asInt()); 
    //... 
} 
1

이 GSON 라이브러리를 사용하여 https://sites.google.com/site/gson/gson-user-guide

public void parseJson() { 
    String jsonStr = "";//nput json String. 
    JsonElement jsonElement = parser.parse(jsonStr); 
    processJsonElement(jsonElement); 
} 


private void processJsonElement(JsonElement e) { 
    if (e.isJsonArray()) { 
     processJsonArray(e.getAsJsonArray()); 
    } else if (e.isJsonNull()) { 
     processJsonNull(e.getAsJsonNull()); 
    } else if (e.isJsonObject()) { 
     processJsonObject(e.getAsJsonObject()); 
    } else if (e.isJsonPrimitive()) { 
     processJsonPrimitive(e.getAsJsonPrimitive()); 
    } 
} 

private void processJsonArray(JsonArray a) { 
    for (JsonElement e : a) { 
     processJsonElement(e); 
    } 
} 

private void processJsonNull(JsonNull n) { 
    System.out.println("null || : " + n); 
} 

private void processJsonObject(JsonObject o) { 
    Set<Map.Entry<String, JsonElement>> members= o.entrySet(); 
    for (Map.Entry<String, JsonElement> e : members) { 
     System.out.println("Processing object member: " + e.getKey()); 
     processJsonElement(e.getValue()); 
    } 
} 

private void processJsonPrimitive(JsonPrimitive p) { 
    System.out.println("Primitive || :" + p); 
} 

또는 잭슨

public void processJson() { 
    ObjectMapper objectMapper = new ObjectMapper(); 
    try { 
     JsonNode node = objectMapper.readTree(jsonStr); 
     System.out.println(node); 
     processNode(node); 
    } catch (IOException e) { 
     throw new RuntimeException(e); 
    } 

} 

private void processNode(JsonNode n) { 
    if (n.isContainerNode()) { 
     processJsonContainer(n.iterator()); 
    } else if (n.isNull()) { 
     System.out.println("Null || :" + n); 
    } else if (n.isNumber()) { 
     System.out.println("Number || :" + n.asDouble()); 
    } else if (n.isBoolean()) { 
     System.out.println("Boolean || :" + n.asBoolean()); 
    } else if (n.isTextual()) { 
     System.out.println("Text || :" + n.asText()); 
    } 
} 

private void processJsonContainer(Iterator<JsonNode> iterator) { 
    while (iterator.hasNext()) { 
     processNode(iterator.next()); 
    } 
} 
관련 문제