Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# get value from deserialized json object

Tags:

json

c#

I'm currently Deserializing a json string using the Newtonsoft.Json nuget packet using the following code:

var data = (JObject)JsonConvert.DeserializeObject(json);

Now I'm receiving an object in the following format:

{{  "meta": {    "rap": 2098,    "count": 5  },  "data": [    {      "name": "Gold Tetramino of Mastery",      "rap": 735,      "uaid": "16601901",      "link": "https://www.roblox.com/Gold-Tetramino-of-Mastery-item?id=5786047",      "img": "https://t4.rbxcdn.com/081337d7ea86e6a406512aaa83bbcdeb",      "serial": "---",      "count": 1    },    {      "name": "Silver Tetramino of Accomplishment",      "rap": 385,      "uaid": "16601900",      "link": "https://www.roblox.com/Silver-Tetramino-of-Accomplishment-item?id=5786026",      "img": "https://t1.rbxcdn.com/60da69cd76f8dad979326f63f4a5b657",      "serial": "---",      "count": 1    },    {      "name": "Subzero Ski Specs",      "rap": 370,      "uaid": "155175547",      "link": "https://www.roblox.com/Subzero-Ski-Specs-item?id=19644587",      "img": "https://t4.rbxcdn.com/8ead2b0418ef418c7650d34103d39b6d",      "serial": "---",      "count": 1    },    {      "name": "Rusty Tetramino of Competence",      "rap": 319,      "uaid": "16601899",      "link": "https://www.roblox.com/Rusty-Tetramino-of-Competence-item?id=5785985",      "img": "https://t2.rbxcdn.com/968ad11ee2f4ee0861ae511c419148c8",      "serial": "---",      "count": 1    },    {      "name": "Bluesteel Egg of Genius",      "rap": 289,      "uaid": "16601902",      "link": "https://www.roblox.com/Bluesteel-Egg-of-Genius-item?id=1533893",      "img": "https://t7.rbxcdn.com/48bf59fe531dd1ff155e455367e52e73",      "serial": "---",      "count": 1    }  ]}}

Now I'm trying to get the following value from it:

"rap": 2098,

I just need 2098 and I've been trying the following code:

string rap = data["rap"].Value<string>();

But sadly this wouldn't work. Does anyone have a idea how to get the value?

like image 963
d4ne Avatar asked Jan 19 '17 22:01

d4ne


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C full form?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is main () in C?

Every C program has a primary function that must be named main . The main function serves as the starting point for program execution. It usually controls program execution by directing the calls to other functions in the program.


6 Answers

I just came to add Yet another way to get 2098:


After having got your object in the way you actually did:

var data = (JObject)JsonConvert.DeserializeObject(json);

The first you have to note is that the value you want to get is itself a value of the property "meta":

Exact location of the desired value inside the Json object

so, first you have to extract the contents of "meta" by simply calling

data["meta"]

and just then you are able to ask for the Value<string> of "rap":

String rap = data["meta"].Value<string>("rap");

which acctually gives you the value you were looking for:

Console.WriteLine(rap); // 2098
like image 195
deczaloth Avatar answered Oct 21 '22 17:10

deczaloth


Instead of declaring as type var and letting the compiler sort it, declare as a dynamic and using the Parse method.

dynamic data = JArray.Parse(json);

Then try

data.meta.rap

To get the internal rap object. I edited from using the deserializeobject method as i incorrectly thought that had the dynamic return type. See here on json.net documentation for more details: http://www.newtonsoft.com/json/help/html/QueryJsonDynamic.htm

like image 22
Chris Watts Avatar answered Oct 21 '22 15:10

Chris Watts


var jobject = (JObject)JsonConvert.DeserializeObject(json);
var jvalue = (JValue)jobject["meta"]["rap"];
Console.WriteLine(jvalue.Value); // 2098
like image 27
Eric Avatar answered Oct 21 '22 16:10

Eric


Try:

var result = data["meta"]["rap"].Value<int>();

or

var result = data.SelectToken("meta.rap").ToString();

or if you don't want to want to pass the whole path in, you could just search for the property like this:

var result = data.Descendants()
                 .OfType<JProperty>()
                 .FirstOrDefault(x => x.Name == "rap")
                 ?.Value;
like image 5
Stuart Avatar answered Oct 21 '22 16:10

Stuart


The value is actually an int type. Try:

int rap = data["rap"].Value<int>();
like image 1
simonlchilds Avatar answered Oct 21 '22 15:10

simonlchilds


string rap = JsonConvert.DeserializeObject<dynamic>(json).meta.rap;
Console.WriteLine(rap); // 2098

If you're not into dynamic (or aren't using .NET 4+), I like how this other answer relies solely on Json.NET's API.

like image 1
William Avatar answered Oct 21 '22 16:10

William