17

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?

1
  • Parse it instead of deserializing if you only need one value Commented Jan 19, 2017 at 22:33

10 Answers 10

8

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;
Sign up to request clarification or add additional context in comments.

1 Comment

I know this was posted a while ago, but this is just another great example from the stack community of knowledge sharing with the multiple different ways to help address this issue. Thank you @stuart!
5

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

3 Comments

There is no Parse method on JsonConvert.
As suggested by stackoverflow.com/questions/47134936/…, I'm using dynamic data = JObject.Parse(json);, which seems to work without exceptions.
in case anyone having a problem with this. it depends on how your Json is returned. try both JArray and Jobject.
5

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

Comments

3
var jobject = (JObject)JsonConvert.DeserializeObject(json);
var jvalue = (JValue)jobject["meta"]["rap"];
Console.WriteLine(jvalue.Value); // 2098

Comments

2

Try to use as following

var test = JsonConvert.DeserializeObject<dynamic>(param);

var testDTO = new TPRDTO();
testDTO.TPR_ID = test.TPR_ID.Value;

Note: For using of JsonConvert class you have to install Newton-Soft from your package-manager

Comments

1

The value is actually an int type. Try:

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

Comments

1
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.

Comments

1

the problem is that you are casting the deserialized json into a JObject. if you want to have the JObject then simple do this:

JObject.Parse(json); 

then you have the JObject and you can access a specific path (for extracting value see this )

you have also another option which is to deserialize your json into a class that you have in your code like this:

var instanceOFTheClass =  JsonConvert.DeserializeObject<YourClassName>(json); 

with the above code you can access any property and values you want.

Comments

1

Use the enumerator to get at the value:

var data = (JObject)JsonConvert.DeserializeObject(json);
var enumerator = data.GetEnumerator();
enumerator.MoveNext();
var dataValue = enumerator.Current.Value.ToString();

1 Comment

Interesting approach @Rian, but sadly it gives the following result: { "rap": 2098, "count": 5 }
0

Just use dynamic representation of object:

dynamic obj = JsonConvert.DeserializeObject(json)
var value = obj.meta.rap;

JObject easily convertable to dynamic type itself. You can either get string or int from this value:

var ivalue = (int)obj.meta.rap;
var svalue = (string)obj.meta.rap;

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.