我正在尝试读取不应区分大小写的Json字符串
dynamic result = JsonConvert.DeserializeObject(response);
for (int i = 0; i < ((Newtonsoft.Json.Linq.JArray)result).Count; i++)
{
Group.id = (string)result[i]["id"];
Group.name = (string)result[i]["GroupName"];
...
...
我希望标签不区分大小写。我如何实现这一目标?我试过了
Group.id = (string)((JObject )result[i].GetValue("ID",StringComparison.CurrentCultureIgnoreCase));
但我收到错误'Newtonsoft.Json.Linq.JObject'不包含'GetValue'的定义。
杰森是:{[
{
"id": "1",
"State": "ON",
"Override": "ON",
"Lightoutput": 80,
"GroupName": "new name for group 1"
},
{
"id": "2",
"State": "ON",
"Override": "OFF",
"Lightoutput": 100,
"GroupName": "group 2"
}
]}
答案 0 :(得分:0)
看起来你有一个Group
课程。假设它看起来像这样:
public class Group
{
public int Id { get; set; }
public string State { get; set; }
public string Override { get; set; }
public int LightOutput { get; set; }
public string GroupName { get; set; }
}
您可以直接将数组反序列化为List<Group>
:
var groups = JsonConvert.DeserializeObject<List<Group>>(json);
但是,如果您出于某种原因确实想使用JObject
和JArray
,则应使用JArray.Parse
并避免使用dynamic
:
JArray arr = JArray.Parse(json);
for(int i = 0; i < arr.Count; i++)
{
var obj = arr[i].ToObject<JObject>();
int id = (int)obj.GetValue(
"ID", StringComparison.CurrentCultureIgnoreCase);
}