我正在使用 www.textlocal.in 的 API,它返回 JSON 格式的对象。
JSON
{
"warnings":[
{
"message":"Number is in DND",
"numbers":"917000000000"
}
],
"balance":900,
"batch_id":311110011,
"cost":1,
"num_messages":1,
"message":{
"num_parts":1,
"sender":"TXTLCL",
"content":"Test1"
},
"receipt_url":"",
"custom":"",
"inDND":[
"917000000000"
],
"messages":[
{
"id":"1350123781",
"recipient":918819437284
}
],
"status":"success"
}
我尝试解析 JSON 的代码:
private void button1_Click(object sender, EventArgs e)
{
var a = JsonConvert.DeserializeObject<List<jsonToObj[]>>(richTextBox1.Text);
}
public class jsonToObj
{
public warnings[] warnings { get; set; }
public int balance { get; set; }
public int batch_id { get; set; }
public int cost { get; set; }
public int num_messages { get; set; }
public message message { get; set; }
public string receipt_url { get; set; }
public string custom { get; set; }
public messages[] messages { get; set; }
public string status { get; set; }
}
public class warnings
{
public string message { get; set; }
public string numbers { get; set; }
}
public class messages
{
public string id { get; set; }
public int recipient { get; set; }
}
public class message
{
public int num_part { get; set; }
public string sender { get; set; }
public string content { get; set; }
}
我收到异常并显示以下消息:
Newtonsoft.Json.JsonSerializationException:'无法反序列化
当前 JSON 对象(例如 {"name":"value"})转换为类型
'System.Collections.Generic.List`1[WindowsFormsApp1.Form2+jsonToObj[]]'
因为该类型需要一个 JSON 数组(例如 [1,2,3])来反序列化
正确。要修复此错误,请将 JSON 更改为 JSON 数组
(例如 [1,2,3])或更改反序列化类型,使其成为正常类型
.NET 类型(例如,不是像整数这样的原始类型,不是集合
类型(如数组或列表),可以从 JSON 反序列化
目的。也可以将 JsonObjectAttribute 添加到类型中以强制它
从 JSON 对象反序列化。路径“警告”,第 1 行,位置
12.'