G
G-Oker
Guest
Hello,
I am trying to get the following JSON response into a list so I can reference on value and get another :
{
"Result": [
{
"Key": "19e848a2-cbb6-4c72-8e24-05ce47da7754",
"Name": "Bistro"
},
{
"Key": "d274591a-06fd-4d50-a973-5a629a3a6d3a",
"Name": "Sports Bar"
},
{
"Key": "b648abd8-da53-4089-a7f9-5ef7ddece38c",
"Name": "Constable Suite"
},
{
"Key": "69a4b8b9-427d-4cca-b62a-62e6cbc8a27c",
"Name": "Haywain Suite"
},
{
"Key": "142a6bc3-86ac-4953-a022-8641f318ffa0",
"Name": "Hotel Lounge Main Bar"
},
{
"Key": "6294ae4e-273b-408a-8e5f-df5853badf90",
"Name": "Garden Room"
},
{
"Key": "f0516ab6-9bce-4f35-aa40-f8f49c464023",
"Name": "Gallery Grill"
}
],
"RID": "1",
"CloudDateTime": "2020-07-04T08:16:36.0000000Z",
"Success": true,
"Message": ""
}
and I have defined my classes as
public class Dept
{
public Result _Result { get; set; }
public string _RID { get; set; }
public DateTime _CloudDateTime { get; set; }
public bool _Success { get; set; }
public string _Message { get; set; }
}
public class Result
{
public string _Key { get; set; }
public string _Name { get; set; }
}
but, when i use the following to grab the data :
List<Dept> Departments = JsonConvert.DeserializeObject<List<Dept>>(response.Content);
foreach (var item in Departments)
{
var _Key = item._Result._Key;
var _Name = item._Result._Name;
richTextBox5.AppendText(_Key + " - " + _Name + Environment.NewLine);
}
I get the message
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[myprogram.Form1+Dept]' 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 'Result', line 1, position 10.
if I change public Results _Result {get; set;} to public List<Result> _Result { get; set; }
I then get swiggles un der _Key and _Name
var _Key = item._Result._Key;
var _Name = item._Result._Name;
saying "does not contain a definition for "_Key" ...."
How do I get eh response into a list so I can then reference the name to the key ie :
if(_Name == "Bistro")
{
var deptKey = theCorrespondingDeptKeyFromList
MessageBox.show(_Name + " " + deptKey)
}
thanks
Continue reading...
I am trying to get the following JSON response into a list so I can reference on value and get another :
{
"Result": [
{
"Key": "19e848a2-cbb6-4c72-8e24-05ce47da7754",
"Name": "Bistro"
},
{
"Key": "d274591a-06fd-4d50-a973-5a629a3a6d3a",
"Name": "Sports Bar"
},
{
"Key": "b648abd8-da53-4089-a7f9-5ef7ddece38c",
"Name": "Constable Suite"
},
{
"Key": "69a4b8b9-427d-4cca-b62a-62e6cbc8a27c",
"Name": "Haywain Suite"
},
{
"Key": "142a6bc3-86ac-4953-a022-8641f318ffa0",
"Name": "Hotel Lounge Main Bar"
},
{
"Key": "6294ae4e-273b-408a-8e5f-df5853badf90",
"Name": "Garden Room"
},
{
"Key": "f0516ab6-9bce-4f35-aa40-f8f49c464023",
"Name": "Gallery Grill"
}
],
"RID": "1",
"CloudDateTime": "2020-07-04T08:16:36.0000000Z",
"Success": true,
"Message": ""
}
and I have defined my classes as
public class Dept
{
public Result _Result { get; set; }
public string _RID { get; set; }
public DateTime _CloudDateTime { get; set; }
public bool _Success { get; set; }
public string _Message { get; set; }
}
public class Result
{
public string _Key { get; set; }
public string _Name { get; set; }
}
but, when i use the following to grab the data :
List<Dept> Departments = JsonConvert.DeserializeObject<List<Dept>>(response.Content);
foreach (var item in Departments)
{
var _Key = item._Result._Key;
var _Name = item._Result._Name;
richTextBox5.AppendText(_Key + " - " + _Name + Environment.NewLine);
}
I get the message
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[myprogram.Form1+Dept]' 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 'Result', line 1, position 10.
if I change public Results _Result {get; set;} to public List<Result> _Result { get; set; }
I then get swiggles un der _Key and _Name
var _Key = item._Result._Key;
var _Name = item._Result._Name;
saying "does not contain a definition for "_Key" ...."
How do I get eh response into a list so I can then reference the name to the key ie :
if(_Name == "Bistro")
{
var deptKey = theCorrespondingDeptKeyFromList
MessageBox.show(_Name + " " + deptKey)
}
thanks
Continue reading...