0

I want to deserialize a json string received from a webpage

I'm using this code to get the json string

WebClient client = new WebClient();
string downloadString = client.DownloadString("http://www.downmasters.com/api.php");

But I'm not sure how to convert the string to an array or something equivalent in C# so that I can use it.

Any ideas please?

3 Answers 3

1

Use the DataContractJsonSerializer class.

Serializes objects to the JavaScript Object Notation (JSON) and deserializes JSON data to objects. This class cannot be inherited.

You will need to create some objects to serialize from and deserialize to and annotate them with the different DataContract attributes.

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

1 Comment

Can you show some coding please, I'm beginner in this and I still don't know how to read msdn efficiently
1

You can use JSON.net to do this, here is an example using WebClient like you are currently.

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

WebClient client = new WebClient();
string downloadString = client.DownloadString("http://www.downmasters.com/api.php");
var data = JsonConvert.DeserializeObject(downloadString );

I recently just finished a web app using ASP.NET MVC 4.0 and I ran into some limitations while using WebClient, instead I opted for HttpWebRequest which gave me more options to fine tune my requests. That said, if all you are doing is using the DownloadString method then WebClient is a great choice.

Comments

0

why not try simply .Split(char[], stringsplitter.none)

you can use chars to split the array into your required type, that's how I always do it.

Comments

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.