Problem in deserializing a JSON object in C#
-
Hi, When I try the following code, I receive an error which says:
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Test.Program+NewsItem]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'data', line 2, position 8.'
On this line:
List items = JsonConvert.DeserializeObject>(json);
Here is my Code:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web.Script.Serialization;namespace Test
{
public class Program
{
public class News
{public IList data { get; set; } } public class NewsItem { public IList title { get; set; } public IList body { get; set; } } public static void Main(string\[\] args) { string json = ""; using (StreamReader r = new StreamReader("data3.json")) { json = r.ReadToEnd(); List items = JsonConvert.DeserializeObject\>(json); } List allNews = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize\>(json); foreach (var item in allNews) { foreach (var record in item.data) { Console.WriteLine(record.title); Console.WriteLine(record.body); } Console.ReadLine(); } } }
}
And here is my JSON sample object:
{
"data": [{
"body": ["part 1-1
", "
part 1-2
"],
"link": "http://www.test.com",
"title": ["sample title 1"]
},
{
"body": ["part 2-1
", "
part 2-2
"],
"link": "http://www.test.com",
"title": ["sample title 2"]
}
]
}Thank you in advance for your time and considera
-
Hi, When I try the following code, I receive an error which says:
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Test.Program+NewsItem]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'data', line 2, position 8.'
On this line:
List items = JsonConvert.DeserializeObject>(json);
Here is my Code:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web.Script.Serialization;namespace Test
{
public class Program
{
public class News
{public IList data { get; set; } } public class NewsItem { public IList title { get; set; } public IList body { get; set; } } public static void Main(string\[\] args) { string json = ""; using (StreamReader r = new StreamReader("data3.json")) { json = r.ReadToEnd(); List items = JsonConvert.DeserializeObject\>(json); } List allNews = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize\>(json); foreach (var item in allNews) { foreach (var record in item.data) { Console.WriteLine(record.title); Console.WriteLine(record.body); } Console.ReadLine(); } } }
}
And here is my JSON sample object:
{
"data": [{
"body": ["part 1-1
", "
part 1-2
"],
"link": "http://www.test.com",
"title": ["sample title 1"]
},
{
"body": ["part 2-1
", "
part 2-2
"],
"link": "http://www.test.com",
"title": ["sample title 2"]
}
]
}Thank you in advance for your time and considera
Your data tag in the JSON is not an array yet your C# code it treating it as one.
System.ItDidntWorkException: Something didn't work as expected. A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave Kreskowiak -
Hi, When I try the following code, I receive an error which says:
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Test.Program+NewsItem]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'data', line 2, position 8.'
On this line:
List items = JsonConvert.DeserializeObject>(json);
Here is my Code:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web.Script.Serialization;namespace Test
{
public class Program
{
public class News
{public IList data { get; set; } } public class NewsItem { public IList title { get; set; } public IList body { get; set; } } public static void Main(string\[\] args) { string json = ""; using (StreamReader r = new StreamReader("data3.json")) { json = r.ReadToEnd(); List items = JsonConvert.DeserializeObject\>(json); } List allNews = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize\>(json); foreach (var item in allNews) { foreach (var record in item.data) { Console.WriteLine(record.title); Console.WriteLine(record.body); } Console.ReadLine(); } } }
}
And here is my JSON sample object:
{
"data": [{
"body": ["part 1-1
", "
part 1-2
"],
"link": "http://www.test.com",
"title": ["sample title 1"]
},
{
"body": ["part 2-1
", "
part 2-2
"],
"link": "http://www.test.com",
"title": ["sample title 2"]
}
]
}Thank you in advance for your time and considera
-
JSON.NET, and most other JSON libraries, will simply ignore any missing properties.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Hi, When I try the following code, I receive an error which says:
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Test.Program+NewsItem]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'data', line 2, position 8.'
On this line:
List items = JsonConvert.DeserializeObject>(json);
Here is my Code:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web.Script.Serialization;namespace Test
{
public class Program
{
public class News
{public IList data { get; set; } } public class NewsItem { public IList title { get; set; } public IList body { get; set; } } public static void Main(string\[\] args) { string json = ""; using (StreamReader r = new StreamReader("data3.json")) { json = r.ReadToEnd(); List items = JsonConvert.DeserializeObject\>(json); } List allNews = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize\>(json); foreach (var item in allNews) { foreach (var record in item.data) { Console.WriteLine(record.title); Console.WriteLine(record.body); } Console.ReadLine(); } } }
}
And here is my JSON sample object:
{
"data": [{
"body": ["part 1-1
", "
part 1-2
"],
"link": "http://www.test.com",
"title": ["sample title 1"]
},
{
"body": ["part 2-1
", "
part 2-2
"],
"link": "http://www.test.com",
"title": ["sample title 2"]
}
]
}Thank you in advance for your time and considera
Farhad Eft wrote:
JsonConvert.DeserializeObject<List<NewsItem>>(json);
Farhad Eft wrote:
new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<List<News>>(json);
The JSON you've posted is neither a
List<NewsItem>
nor aList<News>
; it is simply a singleNews
object.string json = File.ReadAllText("data3.json");
News allNews = JsonConvert.DeserializeObject<News>(json);
foreach (NewsItem item in allNews.data)
{
foreach (string title in item.title)
{
Console.WriteLine(title);
}foreach (string body in item.body) { Console.WriteLine(body); } Console.WriteLine();
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
JSON.NET, and most other JSON libraries, will simply ignore any missing properties.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Thanks, something I need to test. However, looking at the question again, shouldn't it be trying to deserialise int a News class, rather than NewsItem?
Richard MacCutchan wrote:
shouldn't it be trying to deserialise int a News class, rather than NewsItem?
Indeed. As I posted below. :)
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Farhad Eft wrote:
JsonConvert.DeserializeObject<List<NewsItem>>(json);
Farhad Eft wrote:
new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<List<News>>(json);
The JSON you've posted is neither a
List<NewsItem>
nor aList<News>
; it is simply a singleNews
object.string json = File.ReadAllText("data3.json");
News allNews = JsonConvert.DeserializeObject<News>(json);
foreach (NewsItem item in allNews.data)
{
foreach (string title in item.title)
{
Console.WriteLine(title);
}foreach (string body in item.body) { Console.WriteLine(body); } Console.WriteLine();
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
You guys are simply amazing!! Thank you! :)