发布:2023/12/7 15:52:47作者:大数据 来源:大数据 浏览次数:628
.net core json字符串动态读取或解析,json对象JToken读取
需要安装 插件:Newtonsoft.Json.Linq;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using Newtonsoft.Json.Linq; public string JsonConvert() { //这个里面定义的json 字符串 string dataJson = "{total: 9,page: 1,records: 17, rows : [ {id:'1', cell:[ '1','FG001','SFG001','1.000000','2013-04-02 0:00:00' ] },{id:'2', cell:[ '2','FG001','SFG002','1.000000','2013-02-01 0:00:00' ] } ] }”; //首先要引用 系统JObject 类 JObject obj = JObject.Parse(dataJson); //total 值 9 var total = obj["total”].ToString(); //string level_1 = obj["entry”]["field_23”]["level_1”].ToString(); //获取 id:1 var id_1 = obj["rows”][0]["id”].ToString(); //获取 '1','FG001','SFG001','1.000000','2013-04-02 0:00:00' var cell_1 = obj["rows”][0]["cell”].ToString(); //获取上面的 SFG001 var FG001_1 = obj["rows”][0]["cell”][1].ToString(); return FG001_1; } //可以将json数组转换,数据每个对象为JToken JArray jArray = JArray.Parse(obj["rows"].ToString()); JToken["records"].ToString() |
© Copyright 2014 - 2024 柏港建站平台 ejk5.com. 渝ICP备16000791号-4