-2

I have a json String and there is an array inside it I wan to fetch only array part.

JSON String:

{
    "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. Is it possible to do that.

3
  • 3
    @B001ᛦ comments like this are neither friendly nor helpful. A single link to a similar issue would be much appreciated... Commented Jul 24, 2019 at 10:17
  • Can anyone give me a link? where i can find this or comment what to write in google o search that. Commented Jul 24, 2019 at 10:25
  • Have a look at this Commented Jul 24, 2019 at 10:28

3 Answers 3

0

The quick way would be to use Map and unchecked casts to extract the jsonKeywords element. Assuming you read the JSON into inputJson variable:

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}]
Sign up to request clarification or add additional context in comments.

Comments

0

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:

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();

Comments

0

You have to use JSONARRAY to get data array from JSON:

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

Also, Refer This Link

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.