3

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 zxz 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?

Scott Silvi
  • 3,059
  • 5
  • 40
  • 63

3 Answers3

2

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

Community
  • 1
  • 1
Jordy Langen
  • 3,591
  • 4
  • 23
  • 32
1

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.

ashutosh raina
  • 9,228
  • 12
  • 44
  • 80
  • http://stackoverflow.com/questions/1927589/stroring-duplicate-key-value-pair-c may be this will help you out !! – ashutosh raina Sep 11 '11 at 10:47
  • Yeah I've tried returning just the data portion of the json string going with something like `Dictionary results = JsonConvert.DeserializeObject>(result);` (or KeyValuePair) but it just says "Cannot deserialize JSON array into type 'System.Collections.Generic.Dictionary`2[System.String,System.Int32]'.", and I'm not sure where to go :( – Scott Silvi Sep 11 '11 at 10:48
  • may be just making a array of a custom object which has string and integer as its fields(just a thought). – ashutosh raina Sep 11 '11 at 12:01
  • 1
    I ended up just looping through the data object and returning an array of just strings, and using jObject and JArray to turn into a list – Scott Silvi Sep 11 '11 at 16:46
0

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

Scott Silvi
  • 3,059
  • 5
  • 40
  • 63