2011-07-06 200 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/ 恕我直言,它更简单和可用的库。我第一次使用GSON,但后来把我所有的项目都改成了flexjson而不是GSON。

+0

谢谢尤金!这看起来很有趣。下次我处理JSON时,也会看到这一点。 –

0

使用standard json classes在android系统:

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也可以在这里找到: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只需一行代码即可实现。

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

这将反序列化JSON任何物体进入HashMap,不仅仅是Java SE组件组成。我还没有看到可以做到这一点的另一个Java到/从JSON库。

Gson也可以做到这一点,但它需要多行代码。这是一个这样的解决方案。

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复制从我的博客张贴此代码。)