简体   繁体   English

如何使用java和Gson将json字符串转换为json数组

[英]How to convert a json String to json array using java and Gson

I have a json String and there is an array inside it I wan to fetch only array part.我有一个 json 字符串,里面有一个数组,我只想获取数组部分。

JSON String: JSON 字符串:

{
    "jsonKeywords": [
        {
            "xmlNodeKWId": 0,
            "xmlNodeId": 35,
            "nodeKeyword": "dfdfdf",
            "keywordPosition": "Top",
            "keywordPrefix": "",
            "keywordSuffix": "abcd",
            "valueInLine": 0,
            "keywordMatchPattern": "",
            "templateId": 3
        }
    ]
}

I want to convert it into only:我只想将其转换为:

[
        {
            "xmlNodeKWId": 0,
            "xmlNodeId": 35,
            "nodeKeyword": "dfdfdf",
            "keywordPosition": "Top",
            "keywordPrefix": "",
            "keywordSuffix": "abcd",
            "valueInLine": 0,
            "keywordMatchPattern": "",
            "templateId": 3
        }
]

I am using Gson library to handel JSON.我正在使用 Gson 库来处理 JSON。 Is it possible to do that.有没有可能做到这一点。

Response class:响应类:

 public class Response{

    @SerializedName("jsonKeywords")
    private List<JsonKeywordsItem> jsonKeywords;

    public void setJsonKeywords(List<JsonKeywordsItem> jsonKeywords){
        this.jsonKeywords = jsonKeywords;
    }

    public List<JsonKeywordsItem> getJsonKeywords(){
        return jsonKeywords;
    }

    @Override
    public String toString(){
        return 
            "Response{" + 
            "jsonKeywords = '" + jsonKeywords + '\'' + 
            "}";
        }
}

JsonKeywordsItem class: JsonKeywordsItem 类:

public class JsonKeywordsItem {

@SerializedName("keywordSuffix")
private String keywordSuffix;

@SerializedName("valueInLine")
private int valueInLine;

@SerializedName("keywordMatchPattern")
private String keywordMatchPattern;

@SerializedName("nodeKeyword")
private String nodeKeyword;

@SerializedName("keywordPrefix")
private String keywordPrefix;

@SerializedName("keywordPosition")
private String keywordPosition;

@SerializedName("templateId")
private int templateId;

@SerializedName("xmlNodeKWId")
private int xmlNodeKWId;

@SerializedName("xmlNodeId")
private int xmlNodeId;

public void setKeywordSuffix(String keywordSuffix) {
    this.keywordSuffix = keywordSuffix;
}

public String getKeywordSuffix() {
    return keywordSuffix;
}

public void setValueInLine(int valueInLine) {
    this.valueInLine = valueInLine;
}

public int getValueInLine() {
    return valueInLine;
}

public void setKeywordMatchPattern(String keywordMatchPattern) {
    this.keywordMatchPattern = keywordMatchPattern;
}

public String getKeywordMatchPattern() {
    return keywordMatchPattern;
}

public void setNodeKeyword(String nodeKeyword) {
    this.nodeKeyword = nodeKeyword;
}

public String getNodeKeyword() {
    return nodeKeyword;
}

public void setKeywordPrefix(String keywordPrefix) {
    this.keywordPrefix = keywordPrefix;
}

public String getKeywordPrefix() {
    return keywordPrefix;
}

public void setKeywordPosition(String keywordPosition) {
    this.keywordPosition = keywordPosition;
}

public String getKeywordPosition() {
    return keywordPosition;
}

public void setTemplateId(int templateId) {
    this.templateId = templateId;
}

public int getTemplateId() {
    return templateId;
}

public void setXmlNodeKWId(int xmlNodeKWId) {
    this.xmlNodeKWId = xmlNodeKWId;
}

public int getXmlNodeKWId() {
    return xmlNodeKWId;
}

public void setXmlNodeId(int xmlNodeId) {
    this.xmlNodeId = xmlNodeId;
}

public int getXmlNodeId() {
    return xmlNodeId;
}

@Override
public String toString() {
    return
            "JsonKeywordsItem{" +
                    "keywordSuffix = '" + keywordSuffix + '\'' +
                    ",valueInLine = '" + valueInLine + '\'' +
                    ",keywordMatchPattern = '" + keywordMatchPattern + '\'' +
                    ",nodeKeyword = '" + nodeKeyword + '\'' +
                    ",keywordPrefix = '" + keywordPrefix + '\'' +
                    ",keywordPosition = '" + keywordPosition + '\'' +
                    ",templateId = '" + templateId + '\'' +
                    ",xmlNodeKWId = '" + xmlNodeKWId + '\'' +
                    ",xmlNodeId = '" + xmlNodeId + '\'' +
                    "}";
}
}




Gson gson = new Gson();
      Response response = gson.fromJson("Your response string", Response.class);
            List<JsonKeywordsItem> mList = response.getJsonKeywords();

The quick way would be to use Map and unchecked casts to extract the jsonKeywords element.快速的方法是使用Map和 unchecked casts 来提取jsonKeywords元素。 Assuming you read the JSON into inputJson variable:假设您将 JSON 读入inputJson变量:

Gson gson = new Gson();
Map<String, ?> map = gson.fromJson(inputJson, Map.class);
System.out.println(gson.toJson(map.get("jsonKeywords")));

will output:将输出:

[{"xmlNodeKWId":0.0,"xmlNodeId":35.0,"nodeKeyword":"dfdfdf","keywordPosition":"Top","keywordPrefix":"","keywordSuffix":"abcd","valueInLine":0.0,"keywordMatchPattern":"","templateId":3.0}]

You have to use JSONARRAY to get data array from JSON:您必须使用 JSONARRAY 从 JSON 获取数据数组:

JSONObject jsonObject= new JSONObject(jsonResponse);
JSONArray jsonArray = jsonObject.getJSONArray("jsonKeywords");

Also, Refer This Link另外, 请参阅此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM