-1

I have simple json which looks like this :

[  
  {  
    "id":"0",
    "name":"Bob",
    "place":"Colorado",
  },
  {  
    "id":"1",
    "name":"John",
    "place":"Chicago",
  },
  {  
    "id":"2",
    "name":"Marry",
    "place":"Miami",
  }
]

What I want is using Java to create list of strings (List<String>) that contains all 'names'. I have some experience using Gson and I think about something like:

Gson gson = new Gson();
String[] stringArray= gson.fromJson(jsonString, " ".class);

The problem with this method is that I should create some POJO class which I didn`t in this case. Is it any way I can achieve it without creating separate class with this 'name' property ?

alle3x
  • 475
  • 2
  • 6
  • 18
  • what you have tried? there are tons library to map json to pojo object. – Bagus Tesa May 04 '18 at 06:13
  • I'm voting to close this question as off-topic because there is no effort of any kind, no research. Please try something before posting a requirement here. – AxelH May 04 '18 at 06:14
  • I`ve tried using Gson but maybe I missed the right function that can help me – alle3x May 04 '18 at 06:14
  • Maybe we can tell if you [edit] your question with what you have tried. See [ask] please. (You are not a rookie on SO... you should know that). – AxelH May 04 '18 at 06:15
  • Hi @Alle3x, please at least put the code example you have tried and where did you stuck? we can't exactly help you without anything to start with.. sorry.. – Bagus Tesa May 04 '18 at 06:18
  • I added what I have already tries, but as I explained I want to achieve this resulst without creation of new class if this is possible – alle3x May 04 '18 at 06:20
  • hi @all3x, https://stackoverflow.com/a/4527044/4648586 – Bagus Tesa May 04 '18 at 06:40

3 Answers3

1

Using Jackson to parse, and Java 8 Streams API for extracting only the name field; the following may help you:

// Your string
jsonString = "[{ \"id\":\"0\", \"name\":\"Bob\", \"place\":\"Colorado\" }, { \"id\":\"1\", \"name\":\"John\", \"place\":\"Chicago\"}, { \"id\":\"2\", \"name\":\"Marry\", \"place\":\"Miami\" }]";
// using Jackson to parse
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.getTypeFactory();
List<MyInfo> myObjectList = objectMapper.readValue(jsonString, typeFactory.constructCollectionType(List.class, MyInfo.class));

// Java 8 Collections
List<String> nameList = myObjectList.stream().map(MyInfo::getName).collect(Collectors.toList());

Beware, it implies the usage of a MyInfo class representing your a Java class in which Json objects of yours would fit in.

vahdet
  • 6,357
  • 9
  • 51
  • 106
0

You can use JSONArray to get value from key 'name'. Like this:

JSONArray jSONArray = new JSONArray(yourJson);
List<String> list = new ArrayList<>();
for (int i = 0; i < jSONArray.length(); i++) {
  JSONObject object = (JSONObject) jSONArray.get(i);
  String value = object.getString("name");
  System.out.println(value);
  list.add(value);
}
0

You may try the following code snippet,

    import org.json.simple.JSONArray;
    import org.json.simple.JSONObject;
    import org.json.simple.parser.JSONParser;
    import org.json.simple.parser.ParseException;

        List<String> ls = new ArrayList<String>();
        JSONObject jsonObj = new JSONObject();
        JSONArray jsonArr = new JSONArray();
        JSONParser jsonParse = new JSONParser();

        String str =  "[{\"id\": \"0\",\"name\": \"Bob\",\"place\": \"Colorado\"},"
                + "{\"id\": \"1\",\"name\": \"John\",\"place\": \"Chicago\"},"
                + "{\"id\": \"2\",\"name\": \"Marry\",\"place\": \"Miami\"}]";
        try {
            jsonArr= (JSONArray) jsonParse.parse(str); //parsing the JSONArray
            if(jsonArr!=null){
                int arrayLength =jsonArr.size(); //size is 3 here
                for(int i=0;i<arrayLength;i++){
                    jsonObj = (JSONObject) jsonParse.parse(jsonArr.get(i).toString());
                    ls.add(jsonObj.get("name").toString()); //as we need only value of name into the list
                }
                System.out.println(ls);
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }catch(Exception e1){
            e1.printStackTrace();
        }

As you have array, use JSONArray and used jsonParse to avoid any parsing error. I have used json-simple API to acheive the above.

SkPuthumana
  • 73
  • 1
  • 7