Something is making my head in,
I have an API that returns me JSON, the JSON can have a slight different format depending on the endpoint is sending it. Example.
{
"PayLoad": {
"Method1": [
{
"TimeStamp": "2020-06-03T13:25:25",
"Id": 4235411
}
],
"Timestamp": "2020-06-03T13:26:57.1316371+00:00",
"Signature": "113af0a218b0497ff6f160fcd1b13a7b",
"Hookid": "526ed776-2l71-4c2a-b11f-de8cb2057b1c"
}
}
So what I am looking for is to have returned
Method : Method1
TimeStamp: 2020-06-03T13:25:25
id: 4235411
Signature: 113af0a218b0497ff6f160fcd1b13a7b
HookID: 526ed776-2l71-4c2a-b11f-de8cb2057b1c
Can someone please give me an hint?
I've tried a bunch of code examples I've seen, and can't make it work.
Without getting more information it is hard to know exactly what you are asking but I would look at the examples in JSON.net's Linq to JSON documentation:
https://www.newtonsoft.com/json/help/html/LINQtoJSON.htm
https://www.newtonsoft.com/json/help/html/QueryingLINQtoJSON.htm
string json = @"
{
'PayLoad': {
'Method1':
[
{
'TimeStamp': '2020-06-03T13:25:25',
'Id': 4235411
}
],
'Timestamp': '2020-06-03T13:26:57.1316371+00:00',
'Signature': '113af0a218b0497ff6f160fcd1b13a7b',
'Hookid': '526ed776-2l71-4c2a-b11f-de8cb2057b1c'
}
}";
JObject obj = JObject.Parse(json);
string method1_timestamp = (string)obj["PayLoad"]["Method1"][0]["TimeStamp"];
// "2020-06-03T13:25:25"
// Use technique to parse out other values if needed
You can try creating a class compatible with the expected JSON format:
public class Payload
{
public int Id {get; set;}
public Guid HookID {get; set;}
public string Signature {get; set;}
public Method[] Methods {get; set;}
public DateTime TimeStamp {get; set;}
}
public class Method
{
public int Id {get; set;}
public DateTime TimeStamp{get; set;}
}
Than you can parse the JSON data as a Payload object.
Payload payload = JsonConvert.DeserializeObject<Payload>(json);
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.