从JSON获取数据 [英] Getting data from JSON

查看:143
本文介绍了从JSON获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从JSON字符串中获取值,但我很难实现这一点。

 DebugLogId:1750550,RequestId:17505503,Result:
{Code:,DebugLogId:1750550,Message:}
建议:[
{排名:1,得分:60,标题:这是一个测试消息1},
{排名:2,得分:60,标题:这是一个测试消息2}
]}

以什么方式访问建议中的数据是最简单的?我正在使用GSON模块。理想情况下,我想将它全部放在HashMap中。



感谢任何帮助和/或建议!



解决方案

希望这有帮助:



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\:\这是一个测试消息1 \},{\排名\:\2 \,\得分\:\60 \,\标题\\ :\这是一个测试消息2\}]};

调试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结果结果;
@SerializedName(Suggestions)
private List< Suggestion> suggestionList;

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

/ **
* @param debugLogId设置
* /
public final void setDebugLogId(String debugLogId){
this.debugLogId = debugLogId;
}

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

/ **
* @param requestId requestId设置
* /
public final void setRequestId(String requestId){
this.requestId = requestId;
}

/ **
* @返回结果
* /
public final Result getResult(){
return this.result ;
}

/ **
* @param将结果设置为
* /
public final void setResult(Result result){
this.result = result;
}

/ **
* @返回建议列表
* /
public final List< Suggestion> getSuggestionList(){
return this.suggestionList;
}

/ **
* @param suggestionList建议列表设置
* /
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;

/ **
* @返回代码
* /
public final String getCode(){
return this.code;
}

/ **
* @param代码设置
* /
public final void setCode(String code){
this.code = code;
}

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

/ **
* @param debugLogId设置
* /
public final void setDebugLogId(String debugLogId){
this.debugLogId = debugLogId;
}

/ **
* @返回消息
* /
public final String getMessahe(){
return this.messahe ;
}

/ **
* @param messahe设置
* /
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;

/ **
* @返回排名
* /
public final String getRanking(){
return this.ranking;
}

/ **
* @param排名设置
* /
public final void setRanking(String ranking){
this.ranking = ranking;
}

/ **
* @返回分数
* /
public final String getScore(){
return this.score ;
}

/ **
* @param得分设定
* /
public final void setScore(String score){
this.score = score;
}

/ **
* @返回标题
* /
public final String getTitle(){
return this.title ;
}

/ **
* @param标题设置标题
* /
public final void setTitle(String title){
this.title = title;
}

}


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"}         
    ]}

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!

Thanks for any help!

解决方案

Hope this helps:

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;
    }

}

这篇关于从JSON获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆