kendepelchin
kendepelchin

Reputation: 2994

JSON in PHP catch in C#

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

Upvotes: 2

Views: 1135

Answers (2)

Mike Webb
Mike Webb

Reputation: 9003

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

Upvotes: 3

kprobst
kprobst

Reputation: 16651

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

Upvotes: 2

Related Questions