Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
0 like 0 dislike
644 views
in .Net Framework by 66 69 85

How to get a specifice property from a JSON string without convert/parsing it to a class, by using .NET

I only want to get the username property in the JSON example below:

{
  "result": {
    "item": {  
	   "Profile": {
		  "address": "street x",
		  "city": "Riyadh",
		  "id": "56743",
		  "username": "testUser",
		  "email": "test@gmail.com",
		  "phone": "0555555555",
	  }
    }
  }
}

 

Is there an easy approach to obtain a single property without converting or parsing to a model?

 


1 Answer

0 like 0 dislike
by 66 69 85
 
Best answer

Get a property from a JSON string without parsing it to a class

You can get a specific property from JSON using JObject & SelectToken .
Below you can find the solution of the same JSON example:

var data = (JObject)JsonConvert.DeserializeObject(MyJsonString);
string username = data.SelectToken("result.item.Profile.username").Value<string>();
If you don’t ask, the answer is always NO!
...