深圳全飞鸿

标题: Newtonsoft.Json的 JsonConvert [打印本页]

作者: syant    时间: 2024-11-2 17:28
标题: Newtonsoft.Json的 JsonConvert
本帖最后由 syant 于 2024-11-2 17:45 编辑

$emptyObject = new stdClass();



JSON 字符串转对象:
  1. string jsonString = "{\"Name\":\"John\", \"Age\":30}";
  2. Person person = JsonConvert.DeserializeObject<Person>(jsonString);
复制代码
JSON 字符串转动态对象:
  1. string jsonString = "{\"Name\":\"John\", \"Age\":30}";
  2. dynamic obj = JsonConvert.DeserializeObject(jsonString);
  3. Console.WriteLine(obj.Name); // 输出: John
复制代码
JSON 字符串转字典:
  1. string jsonString = "{\"Name\":\"John\", \"Age\":30}";
  2. Dictionary<string, object> dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonString);
  3. Console.WriteLine(dict["Name"]); // 输出: John
复制代码
JSON 字符串转列表:
  1. string jsonString = "[{\"Name\":\"John\", \"Age\":30}, {\"Name\":\"Jane\", \"Age\":25}]";
  2. List<Person> people = JsonConvert.DeserializeObject<List<Person>>(jsonString);
复制代码

作者: syant    时间: 2024-11-2 17:29
Newtonsoft.Json的 JsonConvert可以传两层json的字符串吗?


假设我们有以下嵌套的 JSON 字符串:
  1. {
  2.     "Name": "John",
  3.     "Age": 30,
  4.     "Address": {
  5.         "Street": "123 Main St",
  6.         "City": "Anytown",
  7.         "ZipCode": "12345"
  8.     }
  9. }
复制代码
定义对应的 C# 类
首先,我们需要定义与 JSON 结构相对应的 C# 类:
  1. public class Address
  2. {
  3.     public string Street { get; set; }
  4.     public string City { get; set; }
  5.     public string ZipCode { get; set; }
  6. }

  7. public class Person
  8. {
  9.     public string Name { get; set; }
  10.     public int Age { get; set; }
  11.     public Address Address { get; set; }
  12. }
复制代码



作者: syant    时间: 2024-11-2 17:34
Newtonsoft.Json的 JsonConvert可以解析这样的字符串吗
  1. {
  2.     "Name": "John",
  3.     "Age": 30,
  4.     "Address": [ "Street", "123 Main St"]
  5. }
复制代码
定义对应的 C# 类
首先,我们需要定义与 JSON 结构相对应的 C# 类:
  1. public class Person
  2. {
  3.     public string Name { get; set; }
  4.     public int Age { get; set; }
  5.     public List<string> Address { get; set; }
  6. }
复制代码



作者: syant    时间: 2024-11-2 17:44
验证总结:
1、login函数
用对象接受,data为[]和{}应该都是可以的
  1. public class ResultResponse
  2.     {
  3.         public String code { get; set; }
  4.         public String msg { get; set; }
  5.         public object data { get; set; }
  6. }
复制代码
2、GetLoginUser
是对象{"id":1,"username":"admin","nickname":"Admin"}, et
没有res和msg, 直接用对象接受
  1. string rsc = client.GetLoginUser(Global.CurrentUser.Account);
  2.                         if (!rsc.Contains("ERROR:"))
  3.                         {
  4.                             FaAdmin fa = JsonHelper.JSON2Model<FaAdmin>(rsc);
复制代码



作者: syant    时间: 2024-11-2 17:49
3、GetDepts
返回的数据是[{},{},{}]
用List<对象>承接
  1. List<FaLabTree> faDepts = WebserviceUtil.GetDepts(json);
复制代码
4、GetDataBySql
返回的数据是[{},{},{}]
也可以用DataTable承接
  1. DataTable vars = null;
  2.                 vars = Util.WebserviceUtil.GetDataBySql(sql);
  3.                 if (vars != null)
  4.                 {
  5.                     for (int i = 0; i < vars.Rows.Count; i++)
  6.                     {
  7.                         DataRow row = vars.Rows[i];
  8.                         string[] items = new string[] { row[0].ToString(), row[1].ToString() };
  9.                         ListViewItem item = new ListViewItem(items);
  10.                         listView2.Items.Add(item);
  11.                     }
复制代码



作者: syant    时间: 2024-11-2 18:18
5、GetBarcodeTemplateBase
直接用对象承接
  1. FaBarcodetemplate faBarcodetemplate =  WebserviceUtil.GetBarcodeTemplateBase(tempid);
复制代码
引入了一个可空
public int? template_permax { get;set;}

6、GetBarcodeTemplateBaseByQueryPage

返回多行,用DataTable承接
  1. DataTable dtInfo = WebserviceUtil.GetBarcodeTemplateBaseByQueryPage(pageCurrent.ToString(), pageSize.ToString(), querys);
  2.             
复制代码


7、GetBarcodeTemplateColumnsByTmpId
返回的是[{},{},{}]
用List<FaBarcodetemplatecolumns> 承接

8、GenerateBarcode
我已改为$json=array();
                $json["code"]=$res;
                $json["msg"]=$msg;
                $json["data"]=$data;
??是不是第一次转化后,直接读就可以了?验证List<Dictionary<string, object>> list = (List<Dictionary<string, object>>)rr.data;
  1. ResultResponse rr = WebserviceUtil.GenerateBarcode(pr1);
  2.                     if (rr.code.Equals("error"))
  3.                     {
  4.                         throw new Exception(rr.msg);
  5.                     }
  6.                     List<Dictionary<string, object>> list = JsonHelper.JSON2Model<List<Dictionary<string, object>>>(rr.data.ToString());
  7.                     if (list.Count <= 0)
  8.                     {
  9.                         throw new Exception(i+"没生成标签数据");
  10.                     }
复制代码





作者: syant    时间: 2024-11-2 18:24
8、GetLastPrintList
数据结构:{"Table":[],"Table1":[{"xt":"0"}]}
两个[],直接用Dataset承接
  1. DataSet ds = WebserviceUtil.GetLastPrintList(PintinputReg.TableName, ReferPrintStr);
  2.                         DataTable dtLast = ds.Tables[0];
复制代码






欢迎光临 深圳全飞鸿 (http://www.nagomes.com/disc/) Powered by Discuz! X3.2