2017-08-05 9 views
-4

私はこのjsonデータを持っていますが、 "fine" - > "start_time"の値を取得したいと思います。 私はGsonを使用しています。私は簡単な方法を知りたいだけです。gsonを使ってjsonをパースする方法

{ 
    "result_index": 0, 
    "results": [ 
{ 
    "final": true, 
    "alternatives": [ 
    { 
     "confidence": 0.715, 
     "transcript": "hello how are you holds the medically I hope you are fine " 
    } 
    ], 
    "keywords_result": { 
    "fine": [ 
     { 
     "normalized_text": "fine", 
     "start_time": 5.85, 
     "end_time": 6.27, 
     "confidence": 0.918 
     } 
    ] 
    } 
} 
] 
} 
+0

これを見てください:https://stackoverflow.com/questions/5490789/json-parsing-using-gson-for-java – goblinjuice

+0

何を試しましたか? –

+0

Gson for Javaを使用した[JSON解析]の可能な複製(https://stackoverflow.com/questions/5490789/json-parsing-using-gson-for-java) – mgyongyosi

答えて

0

こんにちは親愛なる友人、

  • あなたは、この目的のためにジャクソンを使用することができます。 2.6.3

com.fasterxml.jackson.core ジャクソン、データバインドそして、あなたはジャクソンライブラリを使用して解析することができます:あなたが依存関係を追加することができ、それはMavenプロジェクトであると仮定すると、あなたの関連オブジェクトにJSON文字列、この場合には、あなたがあなたのようなPOJOを定義します:

public class Staff { 
    private int result_index; 
    private List<Result> results; 
    //getters & setters 
} 
public class Result { 
    private boolean final; 
    private List<Alternative> alternatives; 
    private KeywordsResult keywords_result; 
    //getters & setters 

} 

public class Alternative{ 
    public double confidence; 
    public String transcript; 
    //getters & setters 
} 
public class KeywordsResult{ 
    private List<Fine> fine; 

} 
public class Fine{ 
    private String normalized_text; 
    private double start_time; 
    private double end_time; 
    private double confidence; 
    //getters & setters 
} 

public static void main(String[] args) { 
     ObjectMapper mapper = new ObjectMapper(); 
     try { 
      // Convert JSON string to Object 
      String jsonInString = "{\\r\\n \\\"result_index\\\": 0,\\r\\n \\\"results\\\": [\\r\\n{\\r\\n \\\"final\\\": true,\\r\\n \\\"alternatives\\\": [\\r\\n {\\r\\n  \\\"confidence\\\": 0.715,\\r\\n  \\\"transcript\\\": \\\"hello how are you holds the medically I hope you are fine \\\"\\r\\n }\\r\\n ],\\r\\n \\\"keywords_result\\\": {\\r\\n \\\"fine\\\": [\\r\\n  {\\r\\n  \\\"normalized_text\\\": \\\"fine\\\",\\r\\n  \\\"start_time\\\": 5.85,\\r\\n  \\\"end_time\\\": 6.27,\\r\\n  \\\"confidence\\\": 0.918\\r\\n  }\\r\\n ]\\r\\n }\\r\\n}\\r\\n]\\r\\n}"; 
      Staff json2Object = mapper.readValue(jsonInString, Staff.class); 
      // do whatever you want with object 
     }catch (Exception e){ 
      // handle exception 
     } 
    } 
  • 最後に、あなたを持っていますあなたが好きな小道具を手に入れる準備ができたオブジェクト、一度あなたのpojoを実装してももう問題はありません!
関連する問題