c#将json串转换成数组(使用Newtonsoft)
[ 2022-01-17 17:04:33 | 作者: admin ]
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
string jsonText =
"{'Total':'0','Rows':[{'id':'31','project':'6','project_name':'一卡通','name':'接口测试!','assignedTo':'zhangsan',"
+"'realname':'张三','estStarted':'2016/11/23','realStarted':'2000/01/01','status':'wait'},"
+ "{'id':'32','project':'6','project_name':'一卡通','name':'测试服务器调通!','assignedTo':'lisi',"
+"'realname':'李四','estStarted':'2016/11/23','realStarted':'2016/11/23','status':'wait'}]}";
JObject json1 = (JObject)JsonConvert.DeserializeObject(jsonText);
JArray array = (JArray)json1["Rows"];
int i = array.Count;
string aa="";
foreach (var jObject in array)
{
//赋值属性
aa = jObject["id"].ToString();//获取字符串中id值
}
using Newtonsoft.Json;
string jsonText =
"{'Total':'0','Rows':[{'id':'31','project':'6','project_name':'一卡通','name':'接口测试!','assignedTo':'zhangsan',"
+"'realname':'张三','estStarted':'2016/11/23','realStarted':'2000/01/01','status':'wait'},"
+ "{'id':'32','project':'6','project_name':'一卡通','name':'测试服务器调通!','assignedTo':'lisi',"
+"'realname':'李四','estStarted':'2016/11/23','realStarted':'2016/11/23','status':'wait'}]}";
JObject json1 = (JObject)JsonConvert.DeserializeObject(jsonText);
JArray array = (JArray)json1["Rows"];
int i = array.Count;
string aa="";
foreach (var jObject in array)
{
//赋值属性
aa = jObject["id"].ToString();//获取字符串中id值
}
Newtonsoft.Json.Linq 简单使用
前端代码
$.ajax({
url: top.$.rootUrl + '/BR_RegionalOrder/Dis_PurchaseOrder/SavePurchaseOrder?state=' + state,
data: "{'strJson':'" + JSON.stringify(submitData) + "' }",
type: "POST",
dataType: "json",
contentType: 'application/json',
async: true,
cache: false,
},
});
C# 后端代码(以foreach in遍历为主)
引用 using Newtonsoft.Json.Linq
使用方法1:如果前端传过来的是一个数组Josn,需转成JArray,然后遍历使用
JArray objarr = (JArray)Newtonsoft.Json.JsonConvert.DeserializeObject(strJson);
foreach (JObject ob in objarr) {
//ob type is JObject
取字段名
ob["字段名称"].tostring();
}
使用方法2:如果前台传过来的是一个非数组json,则使用下面代码
JObject ob = (JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(strJson);
取字段名
ob["字段名称"].tostring();
[最后修改由 admin, 于 2022-01-19 15:48:15]

这篇日志没有评论。
此日志不可发表评论。