Reputation: 333
I am attempting to parse my JSON response by taking the response and copy/paste it into Json2CSharp then adding the 2 newly generated classes to the bottom of my current class. (I do not like dealing with multiple classes). The issue that I am having is that when I attempt to access the generated class RootObject
I get an error of
An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll
Additional information: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Test.RootObject' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
Even though the syntax was converted for me, and I did not change it. What do I need to alter so that this becomes valid workable syntax?
static void Main(string[] args)
{
string userid= "186exa";
var url = "https/?rst=" + userid;
var connectionClient = new WebClient();
connectionClient.Headers.Set("H", "XXXXXXXXXXXX");
connectionClient.Headers.Set("uname", "user");
connectionClient.Headers.Set("pswd", "pwd");
var content = connectionClient.DownloadString(url);
}
EDIT
This is the class - will post the JSON shortly
public class List
{
public int id { get; set; }
public string cmu { get; set; }
public int lno { get; set; }
public string clr { get; set; }
public string name { get; set; }
public string origin { get; set; }
public string MajorStyle { get; set; }
public string Style { get; set; }
public string styleImage { get; set; }
public int hid { get; set; }
public string bqty { get; set; }
public int cask { get; set; }
public int local { get; set; }
public string city { get; set; }
}
public class RootObject
{
public string style { get; set; }
public List<List> List { get; set; }
}
This is the retunred JSON
[{"Style":"Cajun","List":[{"id":1225,"cmu":"41.2","lno":10,"name":"Bear","origin":"Lake Sinclair, MO","MajorStyle":"Burn Yo Bottom","Style":"","styleImage":"","hid":1,"bqty":"1.00","cask":0,"local":0,"city":"Amsterdam"}
Upvotes: 0
Views: 1350
Reputation: 846
First, you need your classes match with your JSON response, so your classes should look like
public class RootObject
{
public string Style { get; set; }
public List[] List { get; set; }
}
public class List
{
public int id { get; set; }
public string cmu { get; set; }
public int lno { get; set; }
public string clr { get; set; }
public string name { get; set; }
public string origin { get; set; }
public string MajorStyle { get; set; }
public string Style { get; set; }
public string styleImage { get; set; }
public int hid { get; set; }
public string bqty { get; set; }
public int cask { get; set; }
public int local { get; set; }
public string city { get; set; }
}
After that you need to map the response to an object
using Newtonsoft.Json; // Need this to work with JsonConvert
string json = @"[
{
'Style':'Cajun',
'List':
[
{
'id':1225,
'cmu':'41.2',
'lno':10,
'name':'Bear',
'origin':'Lake Sinclair, MO',
'MajorStyle':'Burn Yo Bottom',
'Style':'',
'styleImage':'',
'hid':1,
'bqty':'1.00',
'cask':0,
'local':0,
'city':'Amsterdam'
}
]
}
]";
RootObject[] response = JsonConvert.DeserializeObject<RootObject[]>(json);
Using the keyword List could lead you to mistakes, because there is also a class for C# called List, so be careful.
Upvotes: 1
Reputation: 1081
There are two things at fault with the code you have posted,
Fixing both of those issues, the code parses correctly in Newtonsoft when passing it the type RootObject[], as per:
var o = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject[]>(s);
Where s is the JSON string.
Upvotes: 1