I called REST API and was able to put the JSON response received back in a String using:
string vResp = new StreamReader(response.GetResponseStream(), Encoding.Default).ReadToEnd();
Below is the result of vResp from Console.WriteLine
{
"Response" : [ {
"UserID " : "23123213",
"UserName " : "dms"
}, {
"UserID " : "542515",
"UserName " : "ark"
}, {
"UserID " : "56546",
"UserName " : "oneim"
}, {
"UserID " : "33336536",
"UserName " : "cyberx"
}, {
"UserID " : "7563624",
"UserName " : "bt"
}, {
"UserID " : "1221414",
"UserName " : "azure"
} ],
"count" : "6"
}
I want to parse each UserID
and UserName
and insert each record in a table but not able to parse correctly. I tried different codes and I am able to separate Response
and count
but not able to reach one level below. Can anyone please help in this?
Create C# class that match with your vResp json result and then then use JSON.NET to deserialize the JSON data to created C# class objects.
You may refer this tool http://json2csharp.com/ to generate C# class based on your Json data.
Here is the code sample how you can parse Json data into C# class. User userObj = JsonConvert.DeserializeObject(vResp);
Now User will have parsing information and you can insert into table.
install Newtonsoft.Json nuget package and try this code
using Newtonsoft.Json;
.....
jsonDeserialized = JsonConvert.DeserializeObject<DataRoot>(json);
classes
public class Response
{
public string UserID { get; set; }
public string UserName { get; set; }
}
public class DataRoot
{
public List<Response> Response { get; set; }
public string count { get; set; }
}
use for each to see the result
foreach (var item in jsonDeserialized.Response)
{
Console.WriteLine ( $"UserID : {item.UserID}, UserName: {item.UserName} ");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.