Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have json output like this and i cannot find the root object.

[

{
"Id": 954,
"FilmId": "ST00000026",
"Title": "The Current War",
"ScreenId": 2,
"PreShowStartTime": "2018-01-25T11:00:00",
},
{
"Id": 955,
"FilmId": "ST00000026",
"Title": "The Current War",
"ScreenId": 2,
"PreShowStartTime": "2018-01-25T13:10:00",
}
]

So try to convert json to c# through DataContract using this

[DataContract]

public class RewardRequest1 : JsonDataContractObject
{
[DataMember(Name = "Id")]
public string Id { get; set; }

[DataMember(Name = "FilmId")]
public string FilmId { get; set; }
}

It returns Null value and i cannot find the root object.

What I have tried:

I tried arry but i cannot get the list. Please help me to fix this.
Posted
Updated 24-Jan-18 6:01am
Comments
F-ES Sitecore 24-Jan-18 9:08am    
There is no root object in that JSON, it is an array of two objects so it's IEnumerable<rewardrequest1> in .net terms.
Karthik_Mahalingam 24-Jan-18 10:04am    
can you post the code.
Member 11415004 28-Jan-18 13:20pm    
Hi here is my deserialize method:

[DataContract]
public class JsonDataContractObject
{
public string ToJson()
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(this.GetType());
Stream s = new MemoryStream();
ser.WriteObject(s, this);
s.Position = 0;
StreamReader sr = new StreamReader(s);
return sr.ReadToEnd();
}
}

And i get data from web api using http client. If i have root object then this "DataContractJsonSerializer" method working fine but my json response dont have root object so idea. Please help.

1 solution

Your JSON has no root object, you've just create an array of 2 objects is all.

If you were to change your JSON to
JavaScript
{  
   "Items":[  
      {  
         "Id":954,
         "FilmId":"ST00000026",
         "Title":"The Current War",
         "ScreenId":2,
         "PreShowStartTime":"2018-01-25T11:00:00"
      },
      {  
         "Id":955,
         "FilmId":"ST00000026",
         "Title":"The Current War",
         "ScreenId":2,
         "PreShowStartTime":"2018-01-25T13:10:00"
      }
   ]
}


Then your root object would be "Items" and your class structure would look like

C#
public class Item
{
    public int Id { get; set; }
    public string FilmId { get; set; }
    public string Title { get; set; }
    public int ScreenId { get; set; }
    public DateTime PreShowStartTime { get; set; }
}

public class RootObject
{
    public List<Item> Items { get; set; }
}


Where you could then, using json.net library, do something like

C#
var mydata = JsonConvert.DeserializeObject<RootObject>(myjsonstring>();


But until you alter your JSON that you've provided, you have no root object.
 
Share this answer
 
Comments
Member 11415004 28-Jan-18 13:17pm    
Hi, Thanks for the update. But json result from web api so i cannot change. Can you help to fix this? and i am using DataContractJsonSerializer. My code:

[DataContract]
public class JsonDataContractObject
{
public string ToJson()
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(this.GetType());
Stream s = new MemoryStream();
ser.WriteObject(s, this);
s.Position = 0;
StreamReader sr = new StreamReader(s);
return sr.ReadToEnd();
}
}

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