Question

I have Json response (named as jsonResultAsText):

{
"Title": "Hi all",
"IsSecret": false,
"Agents": [
    {
        "ID": 3,
        "Name": "John F"
    },
    {
        "ID": 7,
        "Name": "Jack D"
    }
]
}

I want to deserialize it using Google's gson library.

I have classes like:

public class JsonResponse {
    public String Title;
    public boolean IsSecret;

    public List<Agent> Agents;
}

public class Agent {
    public int ID;
    public String Name;
}

I want to get json data into JsonResponse class.

It's the code that I try to achieve:

JsonResponse response = 
                 new Gson().fromJson(jsonResultAsText, JsonResponse.class);

But my Android application gives that classic error: Unfortunately, < app_name > has stopped.

I know that there are plenty examples about serializing/deserializing via gson class, but I could not find similar problem/example.

Thanks in advance.

Was it helpful?

Solution

I solve the problem.

I had forgotten to specify this line on json text I provide (on each element of Agents array):

"Since": "2012-01-07T19:11:01.719"

and in my Agent class:

public class Agent {
    public int ID;
    public String Name;

    public DateTime Since;
}

Sure I have that import: import org.joda.time.DateTime;

When I change data type of Since variable from DateTime to String, problem gone.

Now I can deserialize it.

I am just going to learn how to send DateTime from Asp.Net Web Api to Java via Json.

Thanks for comments.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top