0

I've got an JSON string from my API, looks like this:

[
    {
        "id": "abc",
        "data": {
            "Name": "Peter",
            "Date": "2017/12/01"

        }
    },
    {
        "id": "def",
        "data": {
            "Name": "Tina",
            "Date": "2017/12/20"
        }

    },
    {
        "id": "ghi",
        "data": {
            "Name": "Amy",
            "Date": "2017/12/16"
        }

    }
]

Then, I use (java):

Gson gson = new Gson();
Type resultType = new TypeToken<List<Map<String, Object>>>() {
            }.getType();
List<Map<String, Object>> result = gson.fromJson(info, resultType); 

if I call result.get(0).toString()); then it returned:

{id=abc, data={Name=Peter, Date=2017/12/01}}

if I call result.get(0).get("id").toString(); then it returned

abc

Now I want to get the data of "data", when I call result.get(0).get("data").toString(); then it returned

{Name=Peter, Date=2017/12/01}

Finally I want to get the "Name" info, but when I tried to convert this string to Map, it cause some problem, the code is like this:

Type type = new TypeToken<Map<String, Object>>(){}.getType();
Map<String, Object> myMap = gson.fromJson(str, type);

This doesn't work. I found that maybe the string is not a general type of JSON, it is like "Name=Peter, Date=2017/12/01", but it needs "Name": "Peter", "Date": "2017/12/01" , right? Is that the problem? How can I get the data of Name? Can anyone help me?

Updated: I found that if "Name" = "", then I couldn't get it as string type, I cannot use "data.get("Name");". But I still need it. Anyone can fix it? Thanks.

hsw8906
  • 31
  • 9
  • Why are your dates ending with commas? I think JSON doesn't ends with commas - https://en.m.wikipedia.org/wiki/JSON –  Aug 08 '18 at 04:36

3 Answers3

0

First of all, your JSON is malformed, it shouldn't have a comma after date. and to answer your question, don't use map at all.

If you really want to do it without creating a model and additional classes, do it this way:

Gson gson = new Gson();
Type resultType = new TypeToken<List<JsonObject>>() {}.getType();
List<JsonObject> result = gson.fromJson(info, resultType);
System.out.println(result.get(0).get("data").toString());
JsonObject data = result.get(0).get("data").getAsJsonObject();
System.out.println(data.get("Name"));
Ali
  • 117
  • 9
  • Thank you it worked, but I got another problem. If "Name" = "", then it gonna be like {Name=}, I still need this info. I tried to use "if (data.get("Name")==null)" but it didn't work, do you know how to fix this? – hsw8906 Aug 08 '18 at 04:40
  • just realized your initial json is valid, so check my updated my answer. – Ali Aug 08 '18 at 05:53
  • The previous one you wrote could work, just the problem that it couldn't run data.get("Name").toString() if it has no value. I want it to return "". And this one you updated could not work, my app terminates and I don't know why. – hsw8906 Aug 08 '18 at 07:59
  • i double checked it and it works. please mark this as accepted answer if it worked for you. – Ali Aug 08 '18 at 13:35
0

You can directly convert the response into the POJO/Model class. Check this and this

Sunny
  • 3,134
  • 1
  • 17
  • 31
0

You don't need manual parsing, if you are using Gson. See how-

List<Response> responseList = new Gson().fromJson(yourJson, new TypeToken<List<Response>>() {
    }.getType());
Data data = responseList.get(0).getData();
String id = responseList.get(0).getId();
String date = data.getDate();
String name = data.getName();

Isn't this magic? No manual parsing at all.

Response.java class

public class Response {
    private Data data;
    private String id;

    public void setData(Data data) {
        this.data = data;
    }

    public Data getData() {
        return data;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getId() {
        return id;
    }
}

Data.java class

public class Data {
    private String date;
    private String name;

    public void setDate(String date) {
        this.date = date;
    }

    public String getDate() {
        return date;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

How to generate Pojo classes? So here is several websites jsonschema2pojo. Also many Android Studio plugins available, I use RoboPOJOGenerator.

Community
  • 1
  • 1
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212