Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read specific value from JSON in C#

So I have a JSON string where I just want to read a specific value. How do I just pick "Read me please!" from string below?

var readString = /*Read me please!*/

JSON string:

"{\"aString\":\"Read me please!\"}"

For better understanding, how do I do the same here? (just "Read me please!"):

"{\"Result\":
    {    
    \"aString\":\"Read me please!\",
    \"anotherString\":\"Dont read me!\"
    }    
}"

If both alternative have different solution I would like to know both.

PS: I do not wish to save the value into object/class or so. Just temporary inside var readString.

like image 234
user7399041 Avatar asked Jan 23 '17 14:01

user7399041


People also ask

How can I get specific data from JSON?

Getting a specific property from a JSON response object Instead, you select the exact property you want and pull that out through dot notation. The dot ( . ) after response (the name of the JSON payload, as defined arbitrarily in the jQuery AJAX function) is how you access the values you want from the JSON object.

How to handle JSON data in C++?

So if we want to handle JSON in C++, we have to build the associative array from the ground up. Then we have to tag the values with their types. Is it an integer, a real value (probably return as "double"), boolean, a string? It follows that a JSON C++ class is quite a large chunk of code.

What is Jsonconvert DeserializeObject?

DeserializeObject<T>(String,JsonConverter[]) Deserializes the JSON to the specified . NET type using a collection of JsonConverter. DeserializeObject(String, JsonSerializerSettings) Deserializes the JSON to a .


Video Answer


1 Answers

You could write a model:

public class MyModel
{
    public string AString { get; set; }
}

and then use a JSON serializer such as Json.NET:

string readString = "{\"aString\":\"Read me please!\"}";
MyModel model = JsonConvert.DeserializeObject<MyModel>(readString);
Console.WriteLine(model.AString);

If you don't want to use third party solutions you could use the built-in JavaScriptSerializer class:

string readString = "{\"aString\":\"Read me please!\"}";
MyModel model = new JavaScriptSerializer().Deserialize<MyModel>(readString);
Console.WriteLine(model.AString);

Now assuming you want to handle your second JSON string you could simply adapt your model:

public class Wrapper
{
    public MyModel Result { get; set; }
}

public class MyModel
{
    public string AString { get; set; }
    public string AnotherString { get; set; }
}

and then deserialize to this wrapper class:

string readString = ... the JSON string in your second example ...;
Wrapper wrapper = JsonConvert.DeserializeObject<Wrapper>(readString);
Console.WriteLine(wrapper.Result.AString);
Console.WriteLine(wrapper.Result.AnotherString);

UPDATE:

And if you don't want to deserialize to a model you could directly do this:

string readString = "{\"aString\":\"Read me please!\"}";
var res = (JObject)JsonConvert.DeserializeObject(readString);
Console.WriteLine(res.Value<string>("aString"));

or with the built-in JavaScriptSerializer class:

string readString = "{\"aString\":\"Read me please!\"}";
var serializer = new JavaScriptSerializer();
var res = (IDictionary<string, object>)serializer.DeserializeObject(readString);
Console.WriteLine(res["aString"]);
like image 60
Darin Dimitrov Avatar answered Sep 17 '22 12:09

Darin Dimitrov