Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get value from JSON with JSON.NET

Tags:

json

c#

I try to use http://www.codeplex.com/Json to extract values ​​from a json object.

I use imdbapi.com and they return json like this:

{"Title": "Star Wars", "Year": "1977", "Rated", "PG", "Released", "25 May 1977", "Genre", "Action, Adventure, Fantasy, Sci-Fi "" Director ":" George Lucas "," Writer "," George Lucas "," Actors ":" Mark Hamill, Harrison Ford, Carrie Fisher, Alec Guinness, "" Plot ":" Luke Skywalker leaves his home planet, teams up With Other Rebels, and Tries to save Princess Leia from the evil clutch of Darth hrs 1 min "," Rating ":" 8.8 "," Votes ":" 397318 "," ID ":" tt0076759 "," Response ":" True "} 

How can I pick up such title, rating, Year? and save it to my object?

This line return correct json:

JObject jObject = JObject.Parse (json); 

Now I just need help picking out the data I want. any suggestions?

like image 755
SteelSoft Avatar asked Jan 25 '12 20:01

SteelSoft


People also ask

What is the use of JObject in C#?

The JObject type exposes the following members. Initializes a new instance of the JObject class. Initializes a new instance of the JObject class with the specified content. Initializes a new instance of the JObject class with the specified content.

What is JObject and JToken?

The JToken hierarchy looks like this: JToken - abstract base class JContainer - abstract base class of JTokens that can contain other JTokens JArray - represents a JSON array (contains an ordered list of JTokens) JObject - represents a JSON object (contains a collection of JProperties) JProperty - represents a JSON ...

What is JToken in C#?

JToken is the abstract base class of JObject , JArray , JProperty , and JValue , which represent pieces of JSON data after they have been parsed. JsonToken is an enum that is used by JsonReader and JsonWriter to indicate which type of token is being read or written.

What does Jsonconvert Deserializeobject do?

Deserializes the JSON to the specified . NET type. Deserializes the JSON to the specified . NET type using a collection of JsonConverter.


1 Answers

This should help you http://james.newtonking.com/pages/json-net.aspx

string json = @"{     ""Name"": ""Apple"",     ""Expiry"": new Date(1230422400000),     ""Price"": 3.99,     ""Sizes"": [         ""Small"",         ""Medium"",         ""Large""     ] }";  JObject o = JObject.Parse(json);  //This will be "Apple" string name = (string)o["Name"]; 
like image 139
ThePrimeagen Avatar answered Sep 19 '22 14:09

ThePrimeagen