1

I am retrieving a String from SOAP service to asp.net. I am not able to understand how to parse this string and into what variable type? Array? collection? Object?. The retrieved string looks like this:

<string>
[{"Category":"Mobile Common Needs","CategoryID":"{6039207E61CB}","ProductCount":8,"DisplayName":"Mobile Common Needs"},{"Category":"A Vitamins","CategoryID":"{DB3AD2CE2EAD}","ProductCount":4,"DisplayName":"A Vitamins"},{"Category":"B Complex","CategoryID":"{82133AF08331}","ProductCount":7,"DisplayName":"B Complex"},{"Category":"B Vitamins","CategoryID":"{4BE6939D8F7E}","ProductCount":9,"DisplayName":"B Vitamins"},{"Category":"C Vitamins","CategoryID":"{29D6977A7DCB}","ProductCount":9,"DisplayName":"C Vitamins"},{"Category":"D Vitamins","CategoryID":"{FF8A7E14F1E5}","ProductCount":7,"DisplayName":"D Vitamins"},{"Category":"E Vitamins","CategoryID":"{459AEF26893A}","ProductCount":6,"DisplayName":"E Vitamins"},{"Category":"Health Solutions","CategoryID":"{44C28190872F}","ProductCount":10,"DisplayName":"Health Solutions"},{"Category":"Herb Supplements","CategoryID":"{C24C77EC05EF}","ProductCount":15,"DisplayName":"Herb Supplements"},{"Category":"Minerals","CategoryID":"{A9153D05AAEE}","ProductCount":13,"DisplayName":"Minerals"},{"Category":"Multivitamins","CategoryID":"{79E8951CAC06}","ProductCount":13,"DisplayName":"Multivitamins"},{"Category":"Sleep Aid","CategoryID":"{1D2F16A124BB}","ProductCount":1,"DisplayName":"Sleep Aid"},{"Category":"Supplements","CategoryID":"{9BE59D6B9B23}","ProductCount":22,"DisplayName":"Supplements"},{"Category":"Vitamin Packs","CategoryID":"{9FE0A91C0AA3}","ProductCount":5,"DisplayName":"Vitamin Packs"}]
</string>

1 Answer 1

5

What you are retrieving is a XML with JSON data in it.

If you extract the JSON string from it then there are a couple of ways you can use this return string. You could create a custom class for it with all the fields that are mentioned in the JSON structure (Category, CategoryID, ProductCount, DisplayName) and then serialize the string to objects of your custom class type.

This would look like:

DataContractJsonSerializer serializer = 
    new DataContractJsonSerializer(typeof(YourObjectType));

YourObjectType = (YourObjectType)serializer.ReadObject(str);

You can also use the new dynamic keyword in C#4 like this:

using System.Web.Script.Serialization;
JavaScriptSerializer jss = new JavaScriptSerializer();
var d=jss.Deserialize<dynamic>(str);

Then you won't have to define a class for your objects and you can access the items trough d.Category for example and at runtime .NET will check if the property is available.

Sign up to request clarification or add additional context in comments.

3 Comments

wow, I was struggling on google for 5 hours and you just solved my problem in 5 minutes.. thanks you sir.. ;)
Sorry to bother again, I am now using second method Deserialize<dynamic>. but then i am using foreach loops to extract the data in Key and Value format but is there a way i can use it as class object and simply use it like d.ProductName, Thanks for your help..
Nevermind, I figured it out. Just created a product class and used this List<Product> oList2 = jss.Deserialize<List<Product>>(strProducts);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.