我正在尝试对json字符串进行估算,其中json字符串具有"事件"数组,问题在于当我尝试逐列字符串化时,如下所示,它是尝试找到" field_name"变量,但是数组中的所有对象并非以相同的方式构建,只有其中一些具有" field_name"变量。我无法控制所讨论的API。
我基本上要做什么
dynamic obj = JsonConvert.DeserializeObject(json);
var audits = obj.audits;
List<dynamic> dynamics = JsonConvert.DeserializeObject<List<dynamic>>
(audits.ToString());
List<Event> events = JsonConvert.DeserializeObject<List<Event>>
(dynamics[0].events.ToString());
//The Event class
public class Event
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("value")]
public string Value { get; set; }
[JsonProperty("field_name")]
public string Field_Name { get; set; }
}
我继续得到这个例外:
Newtonsoft.Json.JsonReaderException: Error reading string. Unexpected token:
StartArray. Path '[5].value', line 44, position 15.
json数组类似的东西:
[{
"id":"id",
"field_name":"field_name",
"something":"something"
},{
"id":"id",
"number":"number",
"something":"something"
},{
"id":"id",
"field_name":"field_name",
"something":"something"
},{
"id":"id",
"name":"name",
"something":"something"
}]
您可以使用以下内容使用对象mapper:
private ObjectMapper objectMapper = new ObjectMapper();
List<Event> events = readValue(dynamics[0].events.ToString(), new TypeReference<List<Event>>() {});