'How to parse this json result without making class in C#
How can i parse this json without class in c#. I don't know what to use either an array or anything else.
This is the json result i want to be parsed
{
"success": true,
"status": 200,
"message": "SUCCESS",
"response": {
"message": "Device Registered",
"data": [
{
"ID": "607dbb9e08b4c22a",
"Type_ID": "657911C6-AFA5-4AC4-922F-39E56CCB28CC",
"Name": "Genymotion Google Nexus 5X",
"Active": "1",
"Insert_User": "Default",
"Insert_Date": "2019-10-18 16:33:01.650",
"Update_User": "Default",
"Update_Date": "2019-10-18 16:33:01.650"
}
]
}
}
Solution 1:[1]
Thx everyone, i just figure it out my self. This is what i do.
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public async Task<string> CheckDevice(string id)
{
var uri = new Uri(string.Format(Constant.CheckDevicesRegistration + "id=" + id, string.Empty));
try
{
var response = await client.GetAsync(uri);
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsStringAsync();
var jsonData = (JObject)JsonConvert.DeserializeObject(result);
var message = jsonData["response"]["message"].Value<string>();
resultService = message;
}
}
catch
{
resultService = "Connection-Error";
}
return resultService;
}
Solution 2:[2]
You can check this thread on StackOverflow : Getting nested properties with System.Text.Json
Solution 3:[3]
You can use Json.NET https://www.newtonsoft.com/json
Documentation is here
https://www.newtonsoft.com/json/help/html/SerializingJSON.htm
Short explanation:
For Serialize(object to string):
string text = JsonConvert.SerializeObject(object);
For Deserialize(string to object)
var jsonobject = JsonConvert.DeserializeObject(jsonstring);
If you want to Deserialize Anonymous type, you can use this documentation, it's quite simple
https://www.newtonsoft.com/json/help/html/DeserializeAnonymousType.htm
Solution 4:[4]
You can use dynamic instead of creating an object. But dynamic object cost more source than a normal class. If you have performance concern I don't recommend you; if not here you go:
string json = @"[
{
'Title': 'Json.NET is awesome!',
'Author': {
'Name': 'James Newton-King',
'Twitter': '@JamesNK',
'Picture': '/jamesnk.png'
},
'Date': '2013-01-23T19:30:00',
'BodyHtml': '<h3>Title!</h3>\r\n<p>Content!</p>'
}
]";
dynamic blogPosts = JArray.Parse(json);
dynamic blogPost = blogPosts[0];
string title = blogPost.Title;
Console.WriteLine(title);
// Json.NET is awesome!
string author = blogPost.Author.Name;
Console.WriteLine(author);
// James Newton-King
DateTime postDate = blogPost.Date;
Console.WriteLine(postDate);
// 23/01/2013 7:30:00 p.m.
For more documantation have a look here.
Solution 5:[5]
You can use use JObject to parse json
dynamic response = JObject.Parse(jsonString);
success = response ?.success;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Rubik |
Solution 2 | Dharman |
Solution 3 | cancmrt |
Solution 4 | nzrytmn |
Solution 5 |