Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get some values from a JSON string in C#?

Tags:

json

c#

I have a string and I want to get some values from it.

My strings seem like:

string1:

"{\r\n   \"id\": \"100000280905615\",  \r\n \"name\": \"Jerard Jones\",  \r\n   \"first_name\": \"Jerard\",  \r\n   \"last_name\": \"Jones\",  \r\n   \"link\": \"https://www.facebook.com/Jerard.Jones\",  \r\n   \"username\": \"Jerard.Jones\",  \r\n   \"gender\": \"female\",  \r\n   \"locale\": \"en_US\"\r\n}" 

string2:

"{\r\n   \"id\": \"100000390001929\",   \r\n   \"name\": \"\\u05d1\\u05d2\\u05e8\\u15dc\\u25d9 \\u05d1\\u05e8\\u05d5\\u05e9\",   \r\n   \"first_name\": \"\\u05d4\\u05d2\\u05e7\\u02dc\\u05d9\",   \r\n   \"last_name\": \"\\u05d1\\u05e8\\u05d5\\u05e9\",   \r\n   \"link\": "https://www.facebook.com/people/\\u05d2\\u05d1\\u05e@\\u05dc\\u05d9-\\u05d1\\u05e8\\u05d4\\u05e9/100000390001929\",   \r\n   \"gender\": \"female\",   \r\n   \"locale\": \"he_IL\"\r\n}" 

Unfortunately, there is a situation that a string will be by the same concept, but without some parameters:

string3:

"{\r\n   \"id\": \"100000390001929\",   \r\n   \"last_name\": \"\\u05d1\\u05e8\\u05d5\\u05e9\",   \r\n   \"gender\": \"female\",   \r\n   \"locale\": \"he_IL\"\r\n}" 

How can I get the values of: id, first_name, last_name, gender, locale?

Any help appreciated!

like image 911
Alon Shmiel Avatar asked Jul 12 '13 14:07

Alon Shmiel


People also ask

How do I parse a string in JSON?

Use the JavaScript function JSON.parse() to convert text into a JavaScript object: const obj = JSON.parse('{"name":"John", "age":30, "city":"New York"}'); Make sure the text is in JSON format, or else you will get a syntax error.

How use JSON value in C#?

C# JSON serialize Text. Json; var user = new User("John Doe", "gardener", new MyDate(1995, 11, 30)); var json = JsonSerializer. Serialize(user); Console. WriteLine(json); record MyDate(int year, int month, int day); record User(string Name, string Occupation, MyDate DateOfBirth);

What is JSON parse ()?

parse() The JSON. parse() method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.


1 Answers

Your strings are JSON formatted, so you will need to parse it into a object. For that you can use JSON.NET.

Here is an example on how to parse a JSON string into a dynamic object:

string source = "{\r\n   \"id\": \"100000280905615\", \r\n \"name\": \"Jerard Jones\",  \r\n   \"first_name\": \"Jerard\", \r\n   \"last_name\": \"Jones\", \r\n   \"link\": \"https://www.facebook.com/Jerard.Jones\", \r\n   \"username\": \"Jerard.Jones\", \r\n   \"gender\": \"female\", \r\n   \"locale\": \"en_US\"\r\n}"; dynamic data = JObject.Parse(source); Console.WriteLine(data.id); Console.WriteLine(data.first_name); Console.WriteLine(data.last_name); Console.WriteLine(data.gender); Console.WriteLine(data.locale); 

Happy coding!

like image 94
Rubens Mariuzzo Avatar answered Sep 18 '22 13:09

Rubens Mariuzzo