Click here to Skip to main content
15,885,899 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
{
        "1": {
            "Name": "Test1",
            "Age": "22",
            "Address": "12233",
            "HouseID": "12233433",
            "HouseName": "RESTHouse"
        },
        "2": {
            "Name": "Test2",
            "Age": "22",
            "Address": "12233",
            "HouseID": "12233433",
            "HouseName": "RESTHouse"
        }
    }

    For Above JSON please find the C# class mentioned below 
    public class __invalid_type__1

    For Above JSON please find the C# class mentioned below 
    public class __invalid_type__1
    {
        public string Name { get; set; }
        public string Age { get; set; }
        public string Address { get; set; }
        public string HouseID { get; set; }
        public string HouseName { get; set; }
    }

    public class __invalid_type__2
    {
        public string Name { get; set; }
        public string Age { get; set; }
        public string Address { get; set; }
        public string HouseID { get; set; }
        public string HouseName { get; set; }
    }

public class RootObject
{
    public __invalid_type__1 __invalid_name__1 { get; set; }
    public __invalid_type__2 __invalid_name__2 { get; set; }
}
When i tried to deserialize this JSON i am getting below error

"Cannot deserialize the current JSON object (e.g.enter code here {"name":"value"}) into type 'System.Collections.Generic.List`' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

Please help me to deserialize this JSON with proper class</pre>
Posted
Comments
Sergey Alexandrovich Kryukov 11-Jan-16 15:54pm    
Perhaps you should rather address your root problem, why the root is invalid...
—SA

hmm .. well, if you were the person who put the JSON together, I would re-do it - I dont see the value for the '1', '2' etc - if you need them as some sort of index/ID, they should be in the

C#
{
    public string Name { get; set; }
    public string Age { get; set; }
    public string Address { get; set; }
    public string HouseID { get; set; }
    public string HouseName { get; set; }
}


'class' - that means you deserialise an array of (whatever that class is) to a list/IEnumerable etc - if you have no choice and the JSON is what you get from a 3rd party, well, you may be able to manually step through the JSON (I have my doubts however)
 
Share this answer
 
Comments
EswaranS 8-Jan-16 21:58pm    
I am trying to deserialize the below JSON Response

{
"1": {
"Name": "Test1",
"Age": "22",
"Address": "12233",
"HouseID": "12233433",
"HouseName": "RESTHouse"
},
"2": {
"Name": "Test2",
"Age": "22",
"Address": "12233",
"HouseID": "12233433",
"HouseName": "RESTHouse"
}
}

I want to create a strongly typed object in C# so I can deal with this data. but I don't know how to deal with the id number in this case 1 & 2 as root level, that is the identifier for my object details.

help me create a strongly type class for above JSON and deserialize to it
Garth J Lancaster 8-Jan-16 22:12pm    
I know what you're trying to do - and as the error message indicates, and I have tried to tell you, I dont think you can construct a strongly typed object for your JSON - the best you *may* be able to do is walk through it with code like this (as a starter) and manually construct objects with the various tokens/properties that are parsed

JsonTextReader reader = new JsonTextReader(new StringReader(json));
while (reader.Read())
{
if (reader.Value != null)
{
Console.WriteLine("Token: {0}, Value: {1}", reader.TokenType, reader.Value);
}
else
{
Console.WriteLine("Token: {0}", reader.TokenType);
}
}
EswaranS 8-Jan-16 23:21pm    
As you suggested i have used JsonTextReader i am facing below error in while loop iteration
"
After parsing a value an unexpected character was encountered: :. Path '[0]', line 1, position 35".
you can create class something similer to below
C#
public class Person
{
    public string PersonID { get; set; }
    public string Name { get; set; }
    public string Age { get; set; }
    public string Address { get; set; }
    public string HouseID { get; set; }
    public string HouseName { get; set; }
}

then you can parse json as below
C#
string json = @"{
                ""1"": {
                    ""Name"": ""Test1"",
                    ""Age"": ""22"",
                    ""Address"": ""12233"",
                    ""HouseID"": ""12233433"",
                    ""HouseName"": ""RESTHouse""
                },
                ""2"": {
                    ""Name"": ""Test2"",
                    ""Age"": ""22"",
                    ""Address"": ""12233"",
                    ""HouseID"": ""12233433"",
                    ""HouseName"": ""RESTHouse""
                }
            }";
dynamic stuff = JsonConvert.DeserializeObject(json);
List<Person> persons = new List<Person>();
foreach (var item in stuff)
{
    persons.Add(new Person()
    {
        PersonID = item.Name,
        Address = item.Value.Address,
        Age = item.Value.Age,
        HouseID = item.Value.HouseID,
        Name = item.Value.Name,
        HouseName = item.Value.HouseName
    });
}
 
Share this answer
 
Comments
EswaranS 8-Jan-16 23:41pm    
Thank you so much
DamithSL 9-Jan-16 0:11am    
You are welcome!
EswaranS 11-Jan-16 19:35pm    
I have more scenario ,Let's say if i have list as one of property in that Person class then how iterate in this foreach logic.

Could please clarify this

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