How do I get a continuously variable json data in C#? [closed]
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 hours ago.
Improve this questionI have a constantly changing json data, how can I get this json data?
{
"690160211": {
"bulk_id": "",
"js_data": "690160211|demo",
"user_id": 139309,
"creator": 139309,
"status": 200,
"progress": 0.35122666244700007,
"not_picked_up": false,
"show_extra_waiting_msg": false
}
}
690160211 this part is constantly changing in incoming data. How can I get the data under this data in C#?
I want to retrieve data under json data that is constantly changing.
You can parse your json and deserialized a value of the first (and the only) propery. This is code that is using Newtonsoft.Json (you can install it using a nuget)
using Newtonsoft.Json;
Data data = JObject.Parse(json).Properties().First().Value.ToObject<Data>();
public partial class Data
{
[JsonProperty("bulk_id")]
public string BulkId { get; set; }
[JsonProperty("js_data")]
public string JsData { get; set; }
[JsonProperty("user_id")]
public long UserId { get; set; }
[JsonProperty("creator")]
public long Creator { get; set; }
[JsonProperty("status")]
public long Status { get; set; }
[JsonProperty("progress")]
public double Progress { get; set; }
[JsonProperty("not_picked_up")]
public bool NotPickedUp { get; set; }
[JsonProperty("show_extra_waiting_msg")]
public bool ShowExtraWaitingMsg { get; set; }
}
if for some reasons you need the changing property name, you can get it too
string name = JObject.Parse(json).Properties().First().Name; // "690160211"
With Newtonsoft.Json
you can use JObject.Parse
instead of deserializing to a class like you normally would.
Then with the JObject you can loop through keys and find the randomly generated ones.
https://www.newtonsoft.com/json/help/html/ParseJsonObject.htm
精彩评论