Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I know there are several problem with json but nothing as same as to me. My problem
How to convert this jsonString to c# object. I've tried several time. This unexpected problem made me frustrated. I need to know whats problem with me and what should i do....

{ "status": 200, "status_messages": "Success ", "data": [ { "Alarm And Alert": [ { "0": "1", "1": "Alarm And Alert 1", "2": "0", "id": "1", "name": "Alarm And Alert 1", "islock": "0" }, { "0": "6", "1": "Alarm And Alert 6", "2": "0", "id": "6", "name": "Alarm And Alert 6", "islock": "0" } ] }, { "Animal": [ { "0": "7", "1": "Bird", "2": "0", "id": "7", "name": "Bird", "islock": "0" }, { "0": "13", "1": "Funny Animals", "2": "0", "id": "13", "name": "Funny Animals", "islock": "0" } ] }, { "Dj": [ { "0": "14", "1": "Dj 1", "2": "0", "id": "14", "name": "Dj 1", "islock": "0" }, { "0": "15", "1": "Dj 2", "2": "0", "id": "15", "name": "Dj 2", "islock": "0" }, { "0": "18", "1": "Dj 5", "2": "0", "id": "18", "name": "Dj 5", "islock": "0" } ] }, { "Rap": [ { "0": "71", "1": "Rap Ringtones 1", "2": "0", "id": "71", "name": "Rap Ringtones 1", "islock": "0" }, { "0": "77", "1": "Rap Ringtones 7", "2": "0", "id": "77", "name": "Rap Ringtones 7", "islock": "0" } ] } ] }


My Class:

C#
public  class Category
    {
        public string __invalid_name__0 { get; set; }
        public string id { get; set; }
        public string __invalid_name__1 { get; set; }
        public string name { get; set; }
        public string __invalid_name__2 { get; set; }
        public string islock { get; set; }
    }

public  class CategoryCollection
    {
      public List<category> CategoryRingtone { get; set; }
    }

public  class RootObject
    {
        public int status { get; set; }
        public string status_messages { get; set; }
        public List<categorycollection> data { get; set; }
    }

categoriesJsonString = await Downloader.LoadCategoriesFromServer();
            RootObject rootObject = new RootObject();
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(categoriesJsonString));
            DataContractJsonSerializer ser = new DataContractJsonSerializer(rootObject.GetType());
            rootObject = ser.ReadObject(ms) as RootObject;
            ms.Close();
Posted
Updated 10-Aug-14 2:28am
v3

Hello,

There are plenty of tools that can help you doing this kind of stuff.

First have a look at http://jsonformatter.curiousconcept.com/[^] this will help you understand the organisation of your json string.

And also have a look at http://json2csharp.com/[^]. This will generate the C# classes that match your Json

So in you case this is what you would get:


XML
public class AlarmAndAlert
{
    public string __invalid_name__0 { get; set; }
    public string id { get; set; }
    public string __invalid_name__1 { get; set; }
    public string name { get; set; }
    public string __invalid_name__2 { get; set; }
    public string islock { get; set; }
}

public class Animal
{
    public string __invalid_name__0 { get; set; }
    public string id { get; set; }
    public string __invalid_name__1 { get; set; }
    public string name { get; set; }
    public string __invalid_name__2 { get; set; }
    public string islock { get; set; }
}

public class Datum
{
    public List<AlarmAndAlert> __invalid_name__Alarm And Alert { get; set; }
    public List<Animal> Animal { get; set; }
}

public class RootObject
{
    public int status { get; set; }
    public string status_messages { get; set; }
    public List<Datum> data { get; set; }
}



Valery.
 
Share this answer
 
Comments
suzand 10-Aug-14 8:37am    
@Mr.Valery Possoz if you see my post carefully you will see i've already done what you said. At the last portion of my code snippet i tried to convert jsonString to C# object, my problem is there.
Member 13252808 13-Oct-18 3:18am    
how to Deserialize the given json to the newly created classes ?
The class DataContractJSONSerializer is actually a very good option, but it perfectly works if you serialize using it in first place; then it will give you a Javascript/JSON sample you can further use to make input for deserialization on the .NET side.

It may be not perfectly convenient if data always originate in Javascript, or when the data is already given.

Another approach, especially good for Web development, is using the class System.Web.Script.Serialization.JavaScriptSerializer:
http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer%28v=vs.110%29.aspx[^].

The problem is: how to design a .NET class the way it would be successfully deserialize using the method System.Web.Script.Serialization.JavaScriptSerializer.Deserialize<>(string)? It needs understanding of how JSON code is mapped to .NET collections and arrays. You can find the idea in my recent answer, containing illustration with some source code:
How To Convert object type to C# class object type[^].

If you are interested in such approach but still doubtful about applying it to the case of "multi-level JSON data", your follow-up questions will be welcome.

—SA
 
Share this answer
 
Comments
suzand 11-Aug-14 0:55am    
Actually i need to get it done as early as possible that's why i'm exploring here and there. I need to know whats wrong with me. Mr. SA i will read "How To Convert object type to C# class object type[^]" carefully. Thanks
Sergey Alexandrovich Kryukov 11-Aug-14 1:13am    
Are you trying to say that my suggestion does not solve this problem? If not, but you tried to use this approach and still have some problems, please explain those problems. What wrong with you? I don't know exactly, but I would suggest that you are trying to get listened while not reading/listening others, or not paying enough attention or not trying hard enough to understand. You should understand that you are the one who is experiencing a problem and looking for help; for me this is not a problem at all; it is quite trivial.

Here is what you can do: if the first code sample, JSON, is what you have and cannot change, analyze it as a set of nested arrays/dictionaries and design the .NET class accordingly, using the approach I suggested.

If you can change it, use the approach with DataContractJsonSerializer, but first create some data contract, populate the data graph.

—SA
suzand 11-Aug-14 6:30am    
Hi Mr. SA, You will be very glad to know that I've solved my problem by reading your resource. So special thanks to you. I would like to learn more about JSON, can you give me the reference whatever you like. Thanks again.
Sergey Alexandrovich Kryukov 11-Aug-14 11:42am    
Yes, I'm glad to hear that. Is so, please accept my answer formally (green "Accept" button).

JSON is so simple that there is almost nothing to learn about. For the key idea, please see my answer:
http://www.codeproject.com/Answers/776887/pass-json-data-to-javascript#answer1.

See also:
http://en.wikipedia.org/wiki/JSON,
http://msdn.microsoft.com/en-us/library/ms733127%28v=vs.110%29.aspx.

—SA
satheeshk787 15-Aug-15 2:37am    
no problem for four suggestion sir, i am searched about that but all results for ASP ...that i again askin.....


sorry for my bad English...

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