【发布时间】:2014-08-08 05:40:12
【问题描述】:
我有这个 json 字符串:
[
{f_id:100,r_id:200,count:2,c_id=111},
{f_id:120,r_id:200,count:1,c_id=111 }
]
我想用 newtonsoft.json 解析 json 字符串。
我该怎么做?
我写了这段代码:
private void button1_Click(object sender, EventArgs e)
{
string json = @"[
{f_id:100,r_id:200,count:2,c_id=111},
{f_id:120,r_id:200,count:1,c_id=111 }
]";
List<myClass> tmp = JsonConvert.DeserializeObject<List<myClass>>(json);
int firstFId = tmp[0].f_id;
label1.Text = firstFId.ToString();
label2.Text = tmp[1].f_id.ToString();
}
public class myClass
{
public int f_id { get; set; }
public int r_id { get; set; }
public int count { get; set; }
public int c_id { get; set; }
}
但是有这个错误:
An unhandled exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.dll
Additional information: Invalid JavaScript property identifier character: =. Path '[0].count', line 2, position 32.
【问题讨论】:
-
你想把它解析成一个类,还是只是一个中间对象?
-
有大量示例和已回答的问题。他们不是在帮助您解决问题吗?例如,stackoverflow.com/questions/18242429/…。如果不是,请更具体地说明您面临的确切问题。