I am trying to take the incoming JSON items and bind them to listbox items but I am told by visual studio that I need to do an Array and not Object? I've never had to do this... Anyone know how?
My RootObject:
public class RootObject
{
public string url { get; set; }
public string display { get; set; }
public List<string> genetics { get; set; }
public List<string> price { get; set; }
public List<string> brandMaker { get; set; }
public string form { get; set; }
public string dosornos { get; set; }
public string qty { get; set; }
public string mfg { get; set; }
public string mobURI { get; set; }
}
Note: Genetics, Price, BrandMaker don't actually return anything but a value, like below:
"genetics": [
"typeophere"
],
"price": [
"$1400"
],
JSON FILE/REQUEST BASIC RESULT:
[
{
"url": "N/A",
"display": "",
"genetics": [
"microogiz"
],
"price": [
"96.016"
],
"brandMaker": [
"Oshi Kunti Multikashi, Osaka, JP"
],
"form": "tangent",
"dosornos": "n/a",
"qty": "88G",
"mfg": "SelfMade Industries, USA Dist.",
"mobURI": "n/a"
}
]
My original code:
// Get JSON via WEB
string ProviderURI = goURI;
webClient webClient = new WebClient();
webClient.DownloadStringCompleted += new
DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
webClient.DownloadStringAsync(new Uri(ProviderURI));
void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
{
return;
}
var deserializedJSON = JsonConvert.DeserializeObject<RootObject>(e.Result);
lstBoxResults.ItemsSource = deserializedJSON; // or deserializedJSON.url.ToString();
}
Ok, you mention that genetics and price are arrays, but the returned JSON only contains one item. The rest of your RootObject are simple string properties.
Since you did not post a raw JSON example. Let me present a trimmed down version, to keep things simple and short.
Now we can trim down the RootObject class type. I used Pascal casing for my properties and attributed them with JSON.NET attributes to assist with the deserialing / serializing.
Since I don't know the web service you are calling I just stored this JSON in a text file. Marked it as an embedded resource and read it from there.
Just replace this part with your WebClient call to obtain your JSON data.
Now we can deserialize the JSON into a RootObject instance.
Works as advertised. Now you need to bind it to a ListBox control. If you hover over the ItemSource property of your ListBox instance you'll see that the tooltip mentions that you can bind it to a collection. Well a single rootObject isn't a collection. You can't bind it directly.
This will NOT work. You cannot convert a RootObject instance to a System.Collections.IEnumerable. Which is what ItemsSource is expecting.
Easy fix. Let's create a collection.
This is assuming that your JSON data only returns one rootObject. Only one item will appear in your ListBox.
Now let's suppose your JSON data returns an array of RootObjects. For example an array called "items" which contains a collection of RootItems.
Deserialing this is equally easy. Using JSON.NET obtain the collection of RootObjects.
Now deserialize into a collection (IEnumerable).
Since you now already have a collection of RootObject there is no need to create one yourself. You can directly bind it to the ListBox.
Seems like the JSON data you receive is invalid. Just before parsing it make sure you modify it so that you have a valid JSON object.
For example:
Try this