2017-01-04 43 views
1

JSON file text.jsonの内容をJacksonライブラリを使用して解析しようとしています。大きなJSONファイルを解析するためのJavaメソッドの作成方法

私が望むのは、すべてのキーと値を取得するためのJavaメソッドを作成することですが、これまでのコードでは、JSONファイルの最初のキーと最初の値だけを取得しています。

私は自分のJavaクラスには、次のされていることを確認するためにガイダンスとして使用されるコードスニペット:

public void parse(String json) { 
    JsonFactory factory = new JsonFactory(); 

    ObjectMapper mapper = new ObjectMapper(factory); 
    JsonNode rootNode = mapper.readTree(json); 

    Iterator<Map.Entry<String,JsonNode>> fieldsIterator = rootNode.fields(); 
    while (fieldsIterator.hasNext()) { 

     Map.Entry<String,JsonNode> field = fieldsIterator.next(); 
     System.out.println("Key: " + field.getKey() + "\tValue:" + field.getValue()); 
    } 
} 

そして、私が作成した私のJavaクラスを以下に示します。

import java.io.File; 
import java.io.IOException; 
import java.util.Iterator; 
import java.util.Map; 

import org.codehaus.jackson.JsonFactory; 
import org.codehaus.jackson.JsonGenerationException; 
import org.codehaus.jackson.JsonNode; 
import org.codehaus.jackson.JsonParser; 
import org.codehaus.jackson.map.JsonMappingException; 
import org.codehaus.jackson.map.ObjectMapper; 

public class JacksonStreamExample { 
    public static void main(String[] args) { 
     try { 
      //Create a JsonFactory instance 
      JsonFactory factory = new JsonFactory(); 

      //Create a JsonParser instance to read from file c:\\text.json 
      JsonParser jParser = factory.createJsonParser(new File("c:\\text.json")); 

      /*Create an ObjectMapper instance to provide a pointer 
      * to root node of the tree after reading the JSON 
      */ 
      ObjectMapper mapper = new ObjectMapper(factory); 

      //Create tree from JSON 
      JsonNode rootNode = mapper.readTree(jParser); 

      Iterator<Map.Entry<String,JsonNode>> fieldsIterator = rootNode.getFields(); 
      while (fieldsIterator.hasNext()) { 
       Map.Entry<String,JsonNode> field = fieldsIterator.next(); 
       System.out.println("Key: " + field.getKey() + "\tValue:" + field.getValue()); 
      } 

      jParser.close(); 

     } catch (JsonGenerationException e) { 
      e.printStackTrace(); 
     } catch (JsonMappingException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 
私のEclipseの出力がある

1組(キー値)しか作成しない次の例は、

Key: cells Value:[{"type":"basic.Circle","size":{"width":90,"height":54},"position":{"x":-80,"y":200},"angle":0,"id":"cae4c219-c2cd-4a4b-b50c-0f269963ca24","embeds":"","z":1,"wi_name":"START","wi_displayName":"START","wi_description":"","wi_join":"<None>","wi_split":"<None>","wi_performingRole":"<None>","wi_expected_activity_time":null,"wi_expected_user_time":null,"wi_maximum_activity_time":null,"wi_initial_delay":null,"wi_time_unit":"Seconds","wi_required_transitions_for_AND_JOIN":null,"wi_custom_page":"","attrs":{"circle":{"fill":"#000000","width":50,"height":30,"stroke-width":1,"stroke-dasharray":"0"},"text":{"font-size":10,"text":"START","fill":"#ffffff","font-family":"Arial","stroke":"#000000","stroke-width":0,"font-weight":400}}},{"type":"basic.Circle","size":{"width":90,"height":54},"position":{"x":210,"y":200},"angle":0,"id":"d23133e0-e516-4f72-8127-292545d3d479","embeds":"","z":2,"wi_name":"END","wi_displayName":"END","wi_description":"","wi_join":"<None>","wi_split":"<None>","wi_performingRole":"<None>","wi_expected_activity_time":null,"wi_expected_user_time":null,"wi_maximum_activity_time":null,"wi_initial_delay":null,"wi_time_unit":"Seconds","wi_required_transitions_for_AND_JOIN":null,"wi_custom_page":"","attrs":{"circle":{"fill":"#000000","width":50,"height":30,"stroke-width":1,"stroke-dasharray":"0"},"text":{"font-size":10,"text":"END","fill":"#ffffff","font-family":"Arial","stroke":"#000000","stroke-width":0,"font-weight":400}}},{"type":"basic.Rect","position":{"x":-80,"y":370},"size":{"width":90,"height":54},"angle":0,"id":"a53898a5-c018-45c4-bd3f-4ea4d69f58ed","embeds":"","z":3,"wi_name":"ACTIVITY_1","wi_displayName":"ACTIVITY 1","wi_description":"","wi_join":"<None>","wi_split":"<None>","wi_performingRole":"<None>","wi_expected_activity_time":null,"wi_expected_user_time":null,"wi_maximum_activity_time":null,"wi_initial_delay":null,"wi_time_unit":"Seconds","wi_required_transitions_for_AND_JOIN":null,"wi_custom_page":"","attrs":{"rect":{"width":50,"height":30,"rx":2,"ry":2,"stroke-width":1,"stroke-dasharray":"0"},"text":{"text":"Activity","font-size":10,"font-family":"Arial","stroke":"#000000","stroke-width":0,"font-weight":400}}},{"type":"basic.Rect","position":{"x":220,"y":370},"size":{"width":90,"height":54},"angle":0,"id":"e2bd21f2-508d-44b9-9f68-e374d4fa87ea","embeds":"","z":4,"wi_name":"ACTIVITY_2","wi_displayName":"ACTIVITY 2","wi_description":"","wi_join":"<None>","wi_split":"<None>","wi_performingRole":"<None>","wi_expected_activity_time":null,"wi_expected_user_time":null,"wi_maximum_activity_time":null,"wi_initial_delay":null,"wi_time_unit":"Seconds","wi_required_transitions_for_AND_JOIN":null,"wi_custom_page":"","attrs":{"rect":{"width":50,"height":30,"rx":2,"ry":2,"stroke-width":1,"stroke-dasharray":"0"},"text":{"text":"Workitem","font-size":10,"font-family":"Arial","stroke":"#000000","stroke-width":0,"font-weight":400}}},{"type":"link","source":{"id":"cae4c219-c2cd-4a4b-b50c-0f269963ca24"},"target":{"id":"d23133e0-e516-4f72-8127-292545d3d479"},"router":{"name":"manhattan"},"labels":[{"position":0.5,"attrs":{"text":{"text":"Name"}}}],"id":"60ee7ff7-3a3b-487d-b581-49027e7bebe4","embeds":"","z":5,"attrs":{".marker-source":{"d":"M 10 0 L 0 5 L 10 10 z","transform":"scale(0.001)"},".marker-target":{"d":"M 10 0 L 0 5 L 10 10 z"},".connection":{"stroke":"black"}}},{"type":"link","source":{"id":"a53898a5-c018-45c4-bd3f-4ea4d69f58ed"},"target":{"id":"e2bd21f2-508d-44b9-9f68-e374d4fa87ea"},"router":{"name":"manhattan"},"labels":[{"position":0.5,"attrs":{"text":{"text":"Name"}}}],"id":"cea0d1c2-2c18-4bd7-ba35-d94918c6fc9b","embeds":"","z":6,"attrs":{".marker-source":{"d":"M 10 0 L 0 5 L 10 10 z","transform":"scale(0.001)"},".marker-target":{"d":"M 10 0 L 0 5 L 10 10 z"},".connection":{"stroke":"black"}}}] 

私はちょうどpuすべてのキーと値のペアを取得するには、このコードを使用します。

//Create a JsonFactory instance 
JsonFactory factory = new JsonFactory(); 

//Create a JsonParser instance to read from file c:\\text.json 
JsonParser jParser = factory.createJsonParser(new File("c:\\text.json")); 

/*Create an ObjectMapper instance to provide a pointer 
*to root node of the tree after reading the JSON 
*/ 
ObjectMapper mapper = new ObjectMapper(factory); 

//Create tree from JSON 
JsonNode rootNode = mapper.readTree(jParser); 


Iterator<Map.Entry<String,JsonNode>> fieldsIterator = rootNode.getFields(); 
while (fieldsIterator.hasNext()) { 

    Map.Entry<String,JsonNode> field = fieldsIterator.next(); 
    System.out.println("Key: " + field.getKey() + "\tValue:" + field.getValue()); 
} 

どうすればよいですか?

+1

あなたはJSONを共有することができますか?ここにいない場合、ペーストビンまたは要点に表示されている可能性がありますか? –

+1

私はc:\\ text.jsonの内容を意味しました –

+1

[JAVA - 巨大(超大)JSONファイルを解析するための最善のアプローチ]の可能な複製(http://stackoverflow.com/questions/9390368/java-best-approach) -to-parse-huge-extra-large-json-file) – DimaSan

答えて

0

JSONライブラリを変更して問題を解決しました。

私が働いていた私の最終的なコードは以下の通りですjson-simple-1.1.1

を使用:

package jsontoxml; 

import java.io.*; 

import org.json.simple.parser.JSONParser; 
import org.json.simple.*; 
import java.util.*; 


public class JacksonStreamExample { 

    public static void main(String[] args) { 
     JSONParser parser = new JSONParser(); 
    try { 
     Object obj = parser.parse(new FileReader("text.json")); 
     JSONObject jsonObject = (JSONObject) obj; 

     JSONArray cells = (JSONArray) jsonObject.get("cells"); 
     Iterator<JSONObject> iterator = cells.iterator(); 
     while(iterator.hasNext()){ 
      System.out.println(iterator.next()); 
     } 

    } catch (Exception e) { 

     e.printStackTrace(); 

    } 

    } 

} 
2

Jackson1の代わりにJackson2を使用する必要があります。エラーメッセージが示すように、Jackson1のJsonNodefields()メソッドを実際には持ちません。Jackson2's version doesです。 Jackson1で

、あなたはこのような何かしなければならないでしょう:

public static void main(String[] args) { 
    try { 
     Map source = (new ObjectMapper()).readValue(new File(
       "C:\\json.txt"), Map.class); 

     Map<String, Object> result = new LinkedHashMap<>(); 
     buildFlattenedMap(result, source, null); 
     for (String s : result.keySet()) { 
      System.out.println(s); 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

private static void buildFlattenedMap(Map<String, Object> result, 
     Map<String, Object> source, String path) { 
    for (Map.Entry<String, Object> entry : source.entrySet()) { 
     String key = entry.getKey(); 
     if (hasText(path)) { 
      if (key.charAt(0) == '[') { 
       key = StringUtils.join(path, key); 
      } else { 
       key = StringUtils.join(path, '.', key); 
      } 
     } 
     Object value = entry.getValue(); 
     if (value instanceof String) { 
      result.put(key, value); 
     } else if (value instanceof Map) { 
      // Need a compound key 
      @SuppressWarnings("unchecked") 
      Map<String, Object> map = (Map<String, Object>) value; 
      buildFlattenedMap(result, map, key); 
     } else if (value instanceof Collection) { 
      // Need a compound key 
      @SuppressWarnings("unchecked") 
      Collection<Object> collection = (Collection<Object>) value; 
      int count = 0; 
      for (Object object : collection) { 
       buildFlattenedMap(result, Collections.singletonMap("[" 
         + (count++) + "]", object), key); 
      } 
     } else { 
      result.put(key, value == null ? StringUtils.EMPTY : value); 
     } 
    } 
} 

private static boolean hasText(CharSequence str) { 
    if (StringUtils.isEmpty(str)) { 
     return false; 
    } 
    int strLen = str.length(); 
    for (int i = 0; i < strLen; i++) { 
     if (!Character.isWhitespace(str.charAt(i))) { 
      return true; 
     } 
    } 
    return false; 
} 

Iterator<String> fieldNameIterator = rootNode.getFieldNames(); 

while (fieldNameIterator.hasNext()) { 
    String fieldName = fieldNameIterator.next(); 
    JsonNode fieldValue = rootNode.getFieldValue(fieldName); 

    System.out.println("Key: " + fieldName + "\tValue:" + fieldValue); 
} 
+0

私のコードが変更され、あなたのものが組み込まれました。エラーは削除されますが、コンソールでは何も表示されません。私の編集したコードを見てください。どうもありがとうございました。私は次の警告を持っている点に注意してください:説明\tリソース\tパス型JsonNodeから法getFieldValue(String)を\t JacksonStreamExample.javaを推奨されていません\tタイプ \t場所\t/JsonToXML/srcに\tライン29 \t Javaの問題 –

+0

はこちらJSONファイルをテストしたい場合に備えてhttp://pastebin.com/yvu0mHCR –

+1

@ user6681439:あなたの質問の問題は解決しましたか?はいの場合は、与えられた答えを受け入れるべきです。新しい問題に直面したことを意味する問題が修正されたら、新しい質問をすることができます。 – Henrik

関連する問題