Click here to Skip to main content
16,004,587 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying to convert below json string
{"error":[{"id":"15006","code":"Error CODE","message":"Error Message"}]}
to object and getting null.

What I have tried:

C#
List<error> obj =JsonConvert.DeserializeObject<List<error>>(jsonString);


can anyone help me
Posted
Updated 3-May-16 23:58pm
Comments
Kornfeld Eliyahu Peter 4-May-16 5:48am    
What's wrong?
How 'error' declared?
Why not to read the documentation?
Ehsan Sajjad 4-May-16 5:49am    
can you show your c# class against the json you created?

Your c# classes structure would be like :

C#
public class Error
{
    public string id { get; set; }
    public string code { get; set; }
    public string message { get; set; }
}

public class RootObject
{
    public List<Error> error { get; set; }
}



and now you have to write in api:

C#
RootObject obj =JsonConvert.DeserializeObject<RootObject>(jsonString);


You can always use online tools like json2csharp.comto get the c# classes structure against the json.
 
Share this answer
 
v6
Comments
George Jonsson 4-May-16 5:55am    
There are some typos in your code.
Ehsan Sajjad 4-May-16 6:54am    
for example ??
George Jonsson 4-May-16 7:19am    
For example the errors you corrected.
public List<Error> error { get; set; }
used to be
public List<error> error { get; set; }

(Not easy to write code in comments)
Ehsan Sajjad 4-May-16 8:11am    
oh, i corrected it, i noticed that after adding the reply to your comment. Thanks for pointing out :)
Gangadhar Nagaraju 4-May-16 6:15am    
thank you it worked
Classes

C#
public class MyError
{
    public List<MyErrorDetail> error;
}

public class MyErrorDetail
{
    public string id;
    public string code;
    public string message;
}


Usage

C#
string jsonString = "{\"error\":[{\"id\":\"15006\",\"code\":\"Error CODE\",\"message\":\"Error Message\"}]}";

MyError obj = JsonConvert.DeserializeObject<MyError>(jsonString);
 
Share this answer
 
Comments
Gangadhar Nagaraju 4-May-16 6:15am    
thank you it worked

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900