1

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.

3
  • 1
    There's nothing wrong with the code you have posted and it is the correct way to deserialize your JSON with Gson. Your problem lies elsewhere or your JSON isn't what you think it is. Commented May 15, 2013 at 22:46
  • Have you looked at the stacktrace in logcat? That should give you an idea as to what is happening. Commented May 15, 2013 at 22:47
  • @VivinPaliath LogCat has no message. Commented May 15, 2013 at 23:38

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.