Getting data from JSON(从 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 获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 JSON 获取数据
				
        
 
            
        - 将log4j 1.2配置转换为log4j 2配置 2022-01-01
 - Java包名称中单词分隔符的约定是什么? 2022-01-01
 - 从 finally 块返回时 Java 的奇怪行为 2022-01-01
 - Jersey REST 客户端:发布多部分数据 2022-01-01
 - 如何使用WebFilter实现授权头检查 2022-01-01
 - C++ 和 Java 进程之间的共享内存 2022-01-01
 - Eclipse 插件更新错误日志在哪里? 2022-01-01
 - Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
 - Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
 - value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
 
						
						
						
						
						