Extract values from JSON string c#
I used JSON.stringify to I have the following json string returned from javascript into a C# function:
"{
\"code\":\"OK\",
\"data\":[
[\"adidas\",167],
[\"adidas men's summer run basketball shoe\",35],
[\"adidas shoes\",12],
[\"adidas stan smith men\",9],
[\"adidas golf shoes\",9],
[\"adidas clothing\",9],
[\"adidas z开发者_C百科xz nylon man shoes\",8],
[\"adidas uk\",8],
[\"adidas predator\",8],
[\"adidas perfume\",8],
[\"adidas basketball shoes\",8],
[\"puma and adidas shoes\",7],
[\"mi adidas\",7],
[\"mens discontinued adidas sandals\",7],
[\"climate cool mens discontinued adidas sandals\",7],
[\"china olympic adidas\",7],
[\"china olimpic adidas\",7],
[\"chaussure de foot adidas\",7],
[\"alex white adidas\",7],
[\"adidas superstars\",7]
]}"
Inside of the 'data' object sits an array of string & int values. I want to just get the string values out of the data arrayand into a List. How would I do this?
Try this:
JavaScriptSerializer serializer = new JavaScriptSerializer();
var jsonObject= serializer.Deserialize(json_object);
You can look here for more information:
JavaScriptSerializer.Deserialize - how to change field names
parse it into a dictionary using something like Newtonsoft. Once deserilized ,loop the way you want.
After the comment in the above question,dictionary might have to be replaced by a data structure where your duplicates are allowed.
So it's not buried in the comments:
In my JS, I ended up just looping through my data object:
for(i=0;i<r['data'].length,i++) {
newObject[i] = r['data'][i][0];
}
return JSON.stringify(newObject);
I then used JObject & JArray from the JSON.Net/newtonsoft library. Finally, I looped through my JArray to create an ObservableCollection that I bound to my ListBox ItemsSource.
Scott
精彩评论