Click here to Skip to main content
15,879,535 members
Home / Discussions / C#
   

C#

 
GeneralRe: Find highest number using for-loops and arrays Pin
harold aptroot15-May-17 9:13
harold aptroot15-May-17 9:13 
AnswerRe: Find highest number using for-loops and arrays Pin
Richard MacCutchan15-May-17 20:46
mveRichard MacCutchan15-May-17 20:46 
QuestionUTC Time Conversion Pin
milo-xml15-May-17 6:32
professionalmilo-xml15-May-17 6:32 
AnswerRe: UTC Time Conversion Pin
Richard MacCutchan15-May-17 7:56
mveRichard MacCutchan15-May-17 7:56 
GeneralRe: UTC Time Conversion Pin
milo-xml15-May-17 8:00
professionalmilo-xml15-May-17 8:00 
AnswerRe: UTC Time Conversion Pin
Richard Deeming15-May-17 7:58
mveRichard Deeming15-May-17 7:58 
GeneralRe: UTC Time Conversion Pin
milo-xml15-May-17 8:04
professionalmilo-xml15-May-17 8:04 
QuestionProblem in deserializing a JSON object in C# Pin
Farhad Eft15-May-17 3:07
Farhad Eft15-May-17 3:07 
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<T>) 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<NewsItem> items = JsonConvert.DeserializeObject<List<NewsItem>>(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<NewsItem> data { get; set; }
        }

        public class NewsItem
        {

            public IList<string> title { get; set; }
            public IList<string> body { get; set; }
        }

        public static void Main(string[] args)
        {
            string json = "";
            using (StreamReader r = new StreamReader("data3.json"))
            {
                json = r.ReadToEnd();
                List<NewsItem> items = JsonConvert.DeserializeObject<List<NewsItem>>(json);
            }

            List <News> allNews = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<List<News>>(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": ["<p>part 1-1</p>", "<p>part 1-2</p>"],
			"link": "http://www.test.com",
			"title": ["sample title 1"]
		},
		{
			"body": ["<p>part 2-1</p>", "<p>part 2-2</p>"],
			"link": "http://www.test.com",
			"title": ["sample title 2"]
		}
	]
}


Thank you in advance for your time and consideration.
AnswerRe: Problem in deserializing a JSON object in C# Pin
Dave Kreskowiak15-May-17 4:24
mveDave Kreskowiak15-May-17 4:24 
AnswerRe: Problem in deserializing a JSON object in C# Pin
Richard MacCutchan15-May-17 5:54
mveRichard MacCutchan15-May-17 5:54 
GeneralRe: Problem in deserializing a JSON object in C# Pin
Richard Deeming15-May-17 7:48
mveRichard Deeming15-May-17 7:48 
GeneralRe: Problem in deserializing a JSON object in C# Pin
Richard MacCutchan15-May-17 7:55
mveRichard MacCutchan15-May-17 7:55 
GeneralRe: Problem in deserializing a JSON object in C# Pin
Richard Deeming15-May-17 7:59
mveRichard Deeming15-May-17 7:59 
AnswerRe: Problem in deserializing a JSON object in C# Pin
Richard Deeming15-May-17 7:52
mveRichard Deeming15-May-17 7:52 
GeneralRe: Problem in deserializing a JSON object in C# Pin
Farhad Eft16-May-17 20:21
Farhad Eft16-May-17 20:21 
QuestionC# hierarchical data structure and .NET garbage collection ? Pin
BillWoodruff14-May-17 14:40
professionalBillWoodruff14-May-17 14:40 
AnswerRe: C# hierarchical data structure and .NET garbage collection ? Pin
Luc Pattyn14-May-17 15:25
sitebuilderLuc Pattyn14-May-17 15:25 
GeneralRe: C# hierarchical data structure and .NET garbage collection ? Pin
BillWoodruff14-May-17 20:44
professionalBillWoodruff14-May-17 20:44 
GeneralRe: C# hierarchical data structure and .NET garbage collection ? PinPopular
lmoelleb14-May-17 23:31
lmoelleb14-May-17 23:31 
GeneralRe: C# hierarchical data structure and .NET garbage collection ? Pin
Luc Pattyn14-May-17 23:40
sitebuilderLuc Pattyn14-May-17 23:40 
GeneralRe: C# hierarchical data structure and .NET garbage collection ? Pin
Pete O'Hanlon14-May-17 23:55
mvePete O'Hanlon14-May-17 23:55 
GeneralRe: C# hierarchical data structure and .NET garbage collection ? Pin
BillWoodruff15-May-17 5:40
professionalBillWoodruff15-May-17 5:40 
GeneralRe: C# hierarchical data structure and .NET garbage collection ? Pin
OriginalGriff15-May-17 6:12
mveOriginalGriff15-May-17 6:12 
AnswerRe: C# hierarchical data structure and .NET garbage collection ? Pin
kalberts23-May-17 22:38
kalberts23-May-17 22:38 
QuestionHelp my C# Pin
Arman Petrosyan12-May-17 9:27
Arman Petrosyan12-May-17 9:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.