2

I generate a JSON string in PHP and catch it with httpWebRequest in a .NET windows form application

All works well but I want to use the json.net library to cast that string back to an object (or array).

JsonSerializer serializer = new JsonSerializer();
object result = JsonConvert.DeserializeObject(responseFromServer);

How can i use that object to get the variables from the JSON string?

I've been working this out all day and couldn't find a way to get all values from the JSON string in C#

All help appreciated

2 Answers 2

3

If you create a struct in your code that matches the JSON object you can use JsonConvert.DeserializeObject<T>(String) from the Json.NET library. It's incredibly easy to use and has implementations for pretty much any .NET version.

You can also use DataContracts which requires no 3rd party library. With DataContracts there are some intermediate steps and code you need to include, but here is a site with a pretty detailed explanation: Serialization with DataContracts

Here is a link to a question here on SO that involves Json Serialization using DataContracts: SO Question

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

3 Comments

I think I just found it with the generic jsonConvert.DeserializeObject<>(json);
I have a problem now when I get a list of my database. If I say 'select name,id from companies' in PHP, it returns the correct json string, but how can I catch all those arrays in C#?
What is an example of the Json string?
2

If you're on .NET 4.0 you can use dynamic along with theJavaScriptConverter class to achieve this

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.