1

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.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
ayasha
  • 1,221
  • 5
  • 27
  • 46
  • 1
    What fails? Can you show us the code you are using for the iteration? You can easily use the `values()` method of the `LinkedTreeMap`, which returns a `Collection`. Or you could iterate over the entries, map these to the values and collect them (using, e.g., lamba). – Philipp Feb 09 '17 at 09:00
  • Please post the code of what you are doing with Gson. With jackson, this is really straightforward and I am certain it must be so with Gson as well. – Rohan Prabhu Feb 09 '17 at 09:00

5 Answers5

3

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.

Community
  • 1
  • 1
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);
Philipp
  • 1,289
  • 1
  • 16
  • 37
  • You could use a `TypeToken` with your List like `final Type type = new TypeToken>(){}.getType();` to make sure you have the correct objects in your list and/or `builder.enableComplexMapKeySerialization().create();` – Stefan Lindner Feb 09 '17 at 09:10
1

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.

Akshay
  • 1,161
  • 1
  • 12
  • 33
0

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

utsav_deep
  • 621
  • 6
  • 23
0

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" */
atiqkhaled
  • 386
  • 4
  • 19