Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
CSS
I am new to json and am trying to write code to import a file which looks like the following:
<pre lang="xml">
{
 "name": "John", 
 "joined": "2010/5/4 21:57:34", 
 "title": "Area Manager",
 "languages": [{"id":1,"name":"C#"},{"id":2,"name":"VB.NET"}],

 "speaks":
   [
    {"id":5,"language":"English"},
    {"id":6,"language":"French"}
   ],
 "products":
   [
    {"product_id":"36847f8001cd1","name":"Bolt Free"},
    {"product_id":"36847f8001cd7","name":"Oil4All"}
   ],
 "areas":
   {"countries":
      [
        {"iso_3166_1":"US", "release_date":"1997-02-13"},
        {"iso_3166_1":"DE", "release_date":"1997-05-21"},
      ]
   }
}


Using NuGet I have installed the Newton.Json package. The code to read the file looks like this:
C#
using (System.IO.StreamReader r = new System.IO.StreamReader(src))
{
    string json = r.ReadToEnd();
                EmployeeInfo item = Newtonsoft.Json.JsonConvert.DeserializeObject<EmployeeInfo>(json);
}


The Item class is defined as follows:
C#
public class EmployeeInfo
{
    public string name;
    public DateTime joined;
    public String title;
    public List<Languages> languages;
    public List<Speaks> speaks;
    public List<Products> products;
    public List<Areas> areas;

    public class Languages
    {
        public int id;
        public String name;
    }

    public class Speaks
    {
        public int id;
        public String language;
    }

    public class Products
    {
        public String product_id;
        public String name;
    }

    public class Areas
    {
        public String iso_3166_1;
        public DateTime release_date;
    }
}



When I run the code I get an unhandled exception which says:
“Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[WindowsFormApp.EmployeeInfo+Areas]' 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.”
Can anyone tell me what I am doing wrong and how to get the last item deserialized correctly?
Thanks in advance.
Posted

areas is an object with a property countries which is an array of string and date so you should add
C#
public class arr 
{
        public String iso_3166_1;
        public DateTime release_date;
}

public class Areas
{
    public List<arr> countries;
}


// and your type is 
public class EmployeeInfo
{
    public string name;
    public DateTime joined;
    public String title;
    public List<languages> languages;
    public List<speaks> speaks;
    public List<products> products; 
    public Areas areas;
}
</products></speaks></languages>
 
Share this answer
 
Comments
Kevin Derrick Murphy 26-May-14 9:28am    
Thanks, I just couldn't see what I was missing.
convert it into dataset like this and then you can easily get data from dataset.. :)

How to Convert JSON String into Dataset using c#[^]
 
Share this answer
 
Comments
Kevin Derrick Murphy 26-May-14 9:32am    
Thanks, I will have a look.
Nirav Prabtani 26-May-14 10:09am    
welcome.. :)
you can use http://json2csharp.com/[^] to generate the clases from the json. for example below is the result for your json.
C#
public class Language
{
    public int id { get; set; }
    public string name { get; set; }
}

public class Speak
{
    public int id { get; set; }
    public string language { get; set; }
}

public class Product
{
    public string product_id { get; set; }
    public string name { get; set; }
}

public class Country
{
    public string iso_3166_1 { get; set; }
    public string release_date { get; set; }
}

public class Areas
{
    public List<country> countries { get; set; }
}

public class RootObject
{
    public string name { get; set; }
    public string joined { get; set; }
    public string title { get; set; }
    public List<language> languages { get; set; }
    public List<speak> speaks { get; set; }
    public List<product> products { get; set; }
    public Areas areas { get; set; }
}


Note that you need to change the date time type in generated code. so the final classes will be.
C#
public class Language
{
    public int id { get; set; }
    public string name { get; set; }
}

public class Speak
{
    public int id { get; set; }
    public string language { get; set; }
}

public class Product
{
    public string product_id { get; set; }
    public string name { get; set; }
}

public class Country
{
    public string iso_3166_1 { get; set; }
    public DateTime release_date { get; set; }
}

public class Areas
{
    public List<country> countries { get; set; }
}

public class RootObject
{
    public string name { get; set; }
    public DateTime joined { get; set; }
    public string title { get; set; }
    public List<language> languages { get; set; }
    public List<speak> speaks { get; set; }
    public List<product> products { get; set; }
    public Areas areas { get; set; }
}

Now you can convert json to object like below
C#
RootObject item = Newtonsoft.Json.JsonConvert.DeserializeObject<rootobject>(json);
 
Share this answer
 
v2
Comments
Kevin Derrick Murphy 26-May-14 14:01pm    
Thanks for that. Could be very useful.
C#
JavaScriptSerializer JsonSerializer = new JavaScriptSerializer();
EmployeeInfo OEmployeeInfo = JsonSerializer.Deserialize<EmployeeInfo>(JsonString);
return OEmployeeInfo;
</customformula>
 
Share this answer
 
v2

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