2017-01-09 23 views
0

我試圖解析的JSON file text.json內容的JSON文件的方法。如何添加使用傑克遜庫來解析Java中

我要的是做一個Java方法在下面的代碼來獲得它的所有鍵和值,但在我的代碼,到目前爲止,我只得到了第一個鍵和JSON文件的第一個值。

這裏是我的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"}}}] 

我將如何做到這一點嗎?

+0

的可能的複製[如何在Java解析JSON(http://stackoverflow.com/questions/2591098/如何解析-JSON-in-java) –

+0

你是什麼意思,完全由「所有鍵和值」?你的JSON例子是一個分層結構。你能提供一個單元測試來描述你期望的行爲嗎? – Gustave

+0

我希望能夠遍歷JSON數組和對象內的所有JSON,並獲得所有的key-vaue對。我將如何在我的Java課程中做到這一點?我想念什麼? –

回答

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

    } 

    } 

} 
1

在上面的代碼示例中,嵌套/等級結構不被認爲是JSON值的和它直接打印它作爲field.getValue()。

你必須使用

if(field.getValue().isObject()) 
{ 
    parse(field.getValue()) 
} 

Parse方法可以像然後如下

private void parse(JsonNode jsonNode) 
    { 
     Iterator<Map.Entry<String, JsonNode>> fieldsIterator = jsonNode.getFields(); 
     while (fieldsIterator.hasNext()) 
     { 
      Map.Entry<String, JsonNode> field = fieldsIterator.next(); 

      if (field.getValue().isObject()) 
      { 
       parse(field.getValue()); 
      } 

      System.out.println("Key: " + field.getKey() + "\tValue:" + field.getValue()); 
     } 
    } 

你只是呼籲根節點解析方法來檢查值的類型。

+0

非常感謝你@Kaushik Patel。使用你的代碼後,我的最終課程將如何? –

+0

不要忘記,我希望能夠遍歷JSON數組和對象內的所有JSON,並獲得所有的key-vaue對。我將如何做到這一點? –

+0

用行解析(field.getValue())替換你的Iterator和While循環,並添加解析方法,如上所述 –