JSON Array on Objects to List of String

可紊 提交于 2019-12-24 06:45:00

问题


I have the following JSON

[{
    "rowId": "03 EUR10580000"
}, {
    "rowId": "03 EUR10900001"
}, {
    "rowId": "03 EUR1053RUD*"
}, {
    "rowId": "033331"
}]

and I would like to convert it to a List of String with only the values of the rowId, so in this case like

"03 EUR10580000"
"03 EUR10900001"
"03 EUR1053RUD*"
"033331"

I did it with Gson fromJson but in return I get a List of LinkedTreeMap and when I do a loop in fails. I would like instead a simple List of String.


回答1:


Well, your string is not a json of "list of string". It contains the list of objects. so what you can do is create a class with rowID as a string property.

class Data

  • rowID (type string)

Then you can use Gson to parse this JSON string to List< Data > as used here

or you have to prepare a new json parser manually.




回答2:


If you just want to parse your string quickly using Gson, you can simply create the builder and use the "default" List and Map instances used to represent your JSON in Java. If you want to be more type-safe, or you want to use the parsed instances in a "larger" project, I'd suggest to create a POJO as described in the other answers.

final GsonBuilder gsonBuilder = new GsonBuilder();

// you may want to configure the builder

final Gson gson = gsonBuilder.create();

/*
 * The following only works if you are sure that your JSON looks
 * as described, otherwise List<Object> may be better and a validation,
 * within the iteration.
 */
@SuppressWarnings("unchecked") 
final List<Map<String, String>> list = gson.fromJson("[{ ... }]", List.class);

final List<String> stringList = list.stream()
            .map(m -> m.get("rowId"))
            .collect(Collectors.toList());

System.out.println(stringList);



回答3:


Write a POJO class as

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class RootObject {

    @SerializedName("rowId")
    @Expose
    private String rowId;

    public String getRowId() {
        return rowId;
    }

    public void setRowId(String rowId) {
        this.rowId = rowId;
    }

}

Then just create List<RootObject>and get the values from the POJO.




回答4:


You have:

String str = "[{\"rowId\":\"03 EUR10580000\"},{\"rowId\":\"03 EUR10900001\"},{\"rowId\":\"03 EUR1053RUD*\"},{\"rowId\":\"033331\"}]"

You can do something like this (no need of any external library like gson):

str = str.replace("{\"rowId\":\"","").replace("\"}","").replace("[","").replace("]","");
List<String> rowIDs = str.split(",");

If the string is formatted JSON, you can also trim() each of strings in rowIDs




回答5:


You need to parse json string as JsonArray. Then iterate over JsonArray instance and add each json element to list ls. Follwoing code sinppet is the solution :

    List<String> ls = new ArrayList<String>();
    String json = "[{\"rowId\":\"03 EUR10580000\"},{\"rowId\":\"03 EUR10900001\"},{\"rowId\":\"03 EUR1053RUD*\"},{\"rowId\":\"033331\"}]";
    JsonArray ja = new JsonParser().parse(json).getAsJsonArray();

    for(int i = 0; i < ja.size(); i++) {
       ls.add(ja.get(i).getAsJsonObject().get("rowId").toString());
    }
    for(String rowId : ls) {
       System.out.println(rowId);
    }
    /* output : 
    "03 EUR10580000"
    "03 EUR10900001"
    "03 EUR1053RUD*"
    "033331" */


来源:https://stackoverflow.com/questions/42132081/json-array-on-objects-to-list-of-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!