从 JSON 获取数据

Getting data from JSON(从 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 获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to get large JSON Using REST template Spring MVC without memory issues in java(如何在Java中无内存问题地使用REST模板Spring MVC获取大型JSON)
Using RestTemplate to map JSON to object(使用RestTemplate将JSON映射到对象)
How to map dynamic JSON in JAX-RS(如何在JAX-RS中映射动态JSON)
Convert tuples to json using rdf4j(使用rdf4j将元组转换为json)
How can I store nested JSON data in Room Database? [Room](如何将嵌套的JSON数据存储在Room Database中?[房间])
Is there any way to flatten the nested JSON in spark streaming?(有什么办法可以扁平化电光流媒体中的嵌套JSON吗?)