本文介紹了從 JSON 獲取數據的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在嘗試從這個 JSON 字符串中獲取值,但我很難做到這一點.
I'm trying to get the values out of this JSON string but I'm having a hard time achieving this.
{"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 中.
What way would be easiest to access the data in 'Suggestions'? I'm using the GSON module. Ideally I would like to put it all in a HashMap.
感謝您的幫助和/或建議!
Thanks for any help and/or suggestions!
感謝您的幫助!
推薦答案
希望對你有幫助:
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:
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;
}
}
結果.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:
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;
}
}
這篇關于從 JSON 獲取數據的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!