2011-07-06 23 views
1

私はこのJSON文字列から値を取得しようとしていますが、これを達成するのに苦労しています。JSONからデータを取得

{"DebugLogId":"1750550","RequestId":"17505503","Result": 
{"Code":"","DebugLogId":"1750550","Message":""}, 
    "Suggestions":[ 
     {"Ranking":"1","Score":"60","Title":"This is a test message 1"}, 
     {"Ranking":"2","Score":"60","Title":"This is a test message 2"}   
    ]} 

「提案」のデータにどのようにアクセスするのが最も簡単でしょうか?私はGSONモジュールを使用しています。理想的には、すべてをHashMapに入れたいと思います。

ご協力いただきありがとうございます。

ありがとうございました!

+0

私はあなたを助けるかもしれない以前の今日の答えに何かを。 [ここをクリック](http://stackoverflow.com/questions/6593569/how-to-parse-json-using-gson/6593800#6593800) – Nishant

答えて

1

私はflexjsonライブラリhttp://flexjson.sourceforge.net/ IMHOを使用することをお勧めします、もっとシンプルで使いやすいライブラリ。私はGSONを初めて使用しましたが、GSONの代わりにすべてのプロジェクトをflexjsonに切り替えました。アンドロイドでstandard json classesを使用して

+0

ユージンありがとう!それは面白いようです。また、次回JSONを扱うときも見ていきます。 –

0

JSONObject o = new JSONObject("your string"); 
JSONArray a = o.getJSONArray("Suggestions"); 
int i = 0; 
while (i < a.length()) 
{ 
    o = a.getJSONObject(i); 
    //do something with o, like o.getString("Title") ... 
    ++i; 
} 
+0

おかげさまでWieuxですが、Androidは使用していません。 –

+0

Android固有のAPIではありません。 Androidはjson.org APIを使用しています.json.org APIもあります。http://www.json.org/java/index.html –

5

・ホープ、このことができます:

App.java:

package sg.java.play_sof_json_6596072; 

import com.google.gson.Gson; 

public class App { 
    public static void main(String[] args) { 
     Gson gson = new Gson(); 
     String jsonString = "{\"DebugLogId\":\"1750550\",\"RequestId\":\"17505503\",\"Result\":{\"Code\":\"\",\"DebugLogId\":\"1750550\",\"Message\":\"\"},\"Suggestions\":[{\"Ranking\":\"1\",\"Score\":\"60\",\"Title\":\"This is a test message 1\"},{\"Ranking\":\"2\",\"Score\":\"60\",\"Title\":\"This is a test message 2\"}]}"; 

     Debug obj = (Debug) gson.fromJson(jsonString, Debug.class); 

     System.out.println(obj.getSuggestionList().get(1).getTitle()); 

    } 
} 

Debug.java:

package sg.java.play_sof_json_6596072; 

import java.util.List; 

import com.google.gson.annotations.SerializedName; 

public class Debug { 
    @SerializedName("DebugLogId") 
    private String debugLogId; 
    @SerializedName("RequestId") 
    private String requestId; 
    @SerializedName("Result") 
    private Result result; 
    @SerializedName("Suggestions") 
    private List<Suggestion> suggestionList; 

    /** 
    * @return the debugLogId 
    */ 
    public final String getDebugLogId() { 
     return this.debugLogId; 
    } 

    /** 
    * @param debugLogId the debugLogId to set 
    */ 
    public final void setDebugLogId(String debugLogId) { 
     this.debugLogId = debugLogId; 
    } 

    /** 
    * @return the requestId 
    */ 
    public final String getRequestId() { 
     return this.requestId; 
    } 

    /** 
    * @param requestId the requestId to set 
    */ 
    public final void setRequestId(String requestId) { 
     this.requestId = requestId; 
    } 

    /** 
    * @return the result 
    */ 
    public final Result getResult() { 
     return this.result; 
    } 

    /** 
    * @param result the result to set 
    */ 
    public final void setResult(Result result) { 
     this.result = result; 
    } 

    /** 
    * @return the suggestionList 
    */ 
    public final List<Suggestion> getSuggestionList() { 
     return this.suggestionList; 
    } 

    /** 
    * @param suggestionList the suggestionList to set 
    */ 
    public final void setSuggestionList(List<Suggestion> suggestionList) { 
     this.suggestionList = suggestionList; 
    } 

} 

Result.javaを:

package sg.java.play_sof_json_6596072; 

import com.google.gson.annotations.SerializedName; 

public class Result { 
    @SerializedName("Code") 
    private String code; 
    @SerializedName("DebugLogId") 
    private String debugLogId; 
    @SerializedName("Message") 
    private String messahe; 

    /** 
    * @return the code 
    */ 
    public final String getCode() { 
     return this.code; 
    } 

    /** 
    * @param code the code to set 
    */ 
    public final void setCode(String code) { 
     this.code = code; 
    } 

    /** 
    * @return the debugLogId 
    */ 
    public final String getDebugLogId() { 
     return this.debugLogId; 
    } 

    /** 
    * @param debugLogId the debugLogId to set 
    */ 
    public final void setDebugLogId(String debugLogId) { 
     this.debugLogId = debugLogId; 
    } 

    /** 
    * @return the messahe 
    */ 
    public final String getMessahe() { 
     return this.messahe; 
    } 

    /** 
    * @param messahe the messahe to set 
    */ 
    public final void setMessahe(String messahe) { 
     this.messahe = messahe; 
    } 

} 

Suggestion.java:

package sg.java.play_sof_json_6596072; 

import com.google.gson.annotations.SerializedName; 

public class Suggestion { 
    @SerializedName("Ranking") 
    private String ranking; 
    @SerializedName("Score") 
    private String score; 
    @SerializedName("Title") 
    private String title; 

    /** 
    * @return the ranking 
    */ 
    public final String getRanking() { 
     return this.ranking; 
    } 

    /** 
    * @param ranking the ranking to set 
    */ 
    public final void setRanking(String ranking) { 
     this.ranking = ranking; 
    } 

    /** 
    * @return the score 
    */ 
    public final String getScore() { 
     return this.score; 
    } 

    /** 
    * @param score the score to set 
    */ 
    public final void setScore(String score) { 
     this.score = score; 
    } 

    /** 
    * @return the title 
    */ 
    public final String getTitle() { 
     return this.title; 
    } 

    /** 
    * @param title the title to set 
    */ 
    public final void setTitle(String title) { 
     this.title = title; 
    } 

} 
+0

ありがとう!これは完璧に機能しました! 注釈「@SerializedName」は何をしますか? もう一度おねがいします! –

+1

JSON文字列で使用されるキーと、マップするJavaクラスの属性との間のマッピングを提供します。 –

+0

非常に良い!再度、感謝します。 –

0

理想的には私はHashMapの中でそれをすべて入れたいと思います。

ライブラリを切り替えることができる場合は、Jacksonは1行のコードで実現できます。

Map map = new ObjectMapper().readValue(json, Map.class); 

これはただのJava SE成分からなるHashMap、に任意のJSONオブジェクトをデシリアライズするでしょう。私はまだそれを行うことができる別のJavaから/ JSONライブラリを見ていない。

Gsonでも同じことができますが、コードの行数が増えます。そのような解決策が1つあります。

JsonElement je = new JsonParser().parse(json); 
JsonObject jo = je.getAsJsonObject(); 
Map<String, Object> map = createMapFromJsonObject(jo); 

// ... 

static Map<String, Object> createMapFromJsonObject( 
    JsonObject jo) 
{ 
    Map<String, Object> map = new HashMap<String, Object>(); 
    for (Entry<String, JsonElement> entry : jo.entrySet()) 
    { 
    String key = entry.getKey(); 
    JsonElement value = entry.getValue(); 
    map.put(key, getValueFromJsonElement(value)); 
    } 
    return map; 
} 

static Object getValueFromJsonElement(JsonElement je) 
{ 
    if (je.isJsonObject()) 
    { 
    return createMapFromJsonObject(je.getAsJsonObject()); 
    } 
    else if (je.isJsonArray()) 
    { 
    JsonArray array = je.getAsJsonArray(); 
    List<Object> list = new ArrayList<Object>(array.size()); 
    for (JsonElement element : array) 
    { 
     list.add(getValueFromJsonElement(element)); 
    } 
    return list; 
    } 
    else if (je.isJsonNull()) 
    { 
    return null; 
    } 
    else // must be primitive 
    { 
    JsonPrimitive p = je.getAsJsonPrimitive(); 
    if (p.isBoolean()) return p.getAsBoolean(); 
    if (p.isString()) return p.getAsString(); 
    // else p is number, but don't know what kind 
    String s = p.getAsString(); 
    try 
    { 
     return new BigInteger(s); 
    } 
    catch (NumberFormatException e) 
    { 
     // must be a decimal 
     return new BigDecimal(s); 
    } 
    } 
} 

(私はhttp://programmerbruce.blogspot.com/2011/06/gson-v-jackson.htmlで私のブログの記事から、このコードをコピー。)

関連する問題