深圳全飞鸿
标题: Newtonsoft.Json的 JsonConvert [打印本页]
作者: syant 时间: 2024-11-2 17:28
标题: Newtonsoft.Json的 JsonConvert
本帖最后由 syant 于 2024-11-2 17:45 编辑
$emptyObject = new stdClass();
JSON 字符串转对象:- string jsonString = "{\"Name\":\"John\", \"Age\":30}";
- Person person = JsonConvert.DeserializeObject<Person>(jsonString);
复制代码 JSON 字符串转动态对象:
- string jsonString = "{\"Name\":\"John\", \"Age\":30}";
- dynamic obj = JsonConvert.DeserializeObject(jsonString);
- Console.WriteLine(obj.Name); // 输出: John
复制代码 JSON 字符串转字典:
- string jsonString = "{\"Name\":\"John\", \"Age\":30}";
- Dictionary<string, object> dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonString);
- Console.WriteLine(dict["Name"]); // 输出: John
复制代码 JSON 字符串转列表:
- string jsonString = "[{\"Name\":\"John\", \"Age\":30}, {\"Name\":\"Jane\", \"Age\":25}]";
- List<Person> people = JsonConvert.DeserializeObject<List<Person>>(jsonString);
复制代码
作者: syant 时间: 2024-11-2 17:29
Newtonsoft.Json的 JsonConvert可以传两层json的字符串吗?
假设我们有以下嵌套的 JSON 字符串:
- {
- "Name": "John",
- "Age": 30,
- "Address": {
- "Street": "123 Main St",
- "City": "Anytown",
- "ZipCode": "12345"
- }
- }
复制代码 定义对应的 C# 类首先,我们需要定义与 JSON 结构相对应的 C# 类:
- public class Address
- {
- public string Street { get; set; }
- public string City { get; set; }
- public string ZipCode { get; set; }
- }
- public class Person
- {
- public string Name { get; set; }
- public int Age { get; set; }
- public Address Address { get; set; }
- }
复制代码
作者: syant 时间: 2024-11-2 17:34
Newtonsoft.Json的 JsonConvert可以解析这样的字符串吗- {
- "Name": "John",
- "Age": 30,
- "Address": [ "Street", "123 Main St"]
- }
复制代码 定义对应的 C# 类首先,我们需要定义与 JSON 结构相对应的 C# 类:
- public class Person
- {
- public string Name { get; set; }
- public int Age { get; set; }
- public List<string> Address { get; set; }
- }
复制代码
作者: syant 时间: 2024-11-2 17:44
验证总结:
1、login函数
用对象接受,data为[]和{}应该都是可以的
- public class ResultResponse
- {
- public String code { get; set; }
- public String msg { get; set; }
- public object data { get; set; }
- }
复制代码 2、GetLoginUser
是对象{"id":1,"username":"admin","nickname":"Admin"}, et
没有res和msg, 直接用对象接受
- string rsc = client.GetLoginUser(Global.CurrentUser.Account);
- if (!rsc.Contains("ERROR:"))
- {
- FaAdmin fa = JsonHelper.JSON2Model<FaAdmin>(rsc);
复制代码
作者: syant 时间: 2024-11-2 17:49
3、GetDepts
返回的数据是[{},{},{}]
用List<对象>承接
- List<FaLabTree> faDepts = WebserviceUtil.GetDepts(json);
复制代码 4、GetDataBySql
返回的数据是[{},{},{}]
也可以用DataTable承接
- DataTable vars = null;
- vars = Util.WebserviceUtil.GetDataBySql(sql);
- if (vars != null)
- {
- for (int i = 0; i < vars.Rows.Count; i++)
- {
- DataRow row = vars.Rows[i];
- string[] items = new string[] { row[0].ToString(), row[1].ToString() };
- ListViewItem item = new ListViewItem(items);
- listView2.Items.Add(item);
- }
复制代码
作者: syant 时间: 2024-11-2 18:18
5、GetBarcodeTemplateBase
直接用对象承接
- FaBarcodetemplate faBarcodetemplate = WebserviceUtil.GetBarcodeTemplateBase(tempid);
复制代码 引入了一个可空
public int? template_permax { get;set;}
6、GetBarcodeTemplateBaseByQueryPage
返回多行,用DataTable承接
- DataTable dtInfo = WebserviceUtil.GetBarcodeTemplateBaseByQueryPage(pageCurrent.ToString(), pageSize.ToString(), querys);
-
复制代码
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;
- ResultResponse rr = WebserviceUtil.GenerateBarcode(pr1);
- if (rr.code.Equals("error"))
- {
- throw new Exception(rr.msg);
- }
- List<Dictionary<string, object>> list = JsonHelper.JSON2Model<List<Dictionary<string, object>>>(rr.data.ToString());
- if (list.Count <= 0)
- {
- throw new Exception(i+"没生成标签数据");
- }
复制代码
作者: syant 时间: 2024-11-2 18:24
8、GetLastPrintList
数据结构:{"Table":[],"Table1":[{"xt":"0"}]}
两个[],直接用Dataset承接
- DataSet ds = WebserviceUtil.GetLastPrintList(PintinputReg.TableName, ReferPrintStr);
- DataTable dtLast = ds.Tables[0];
复制代码
欢迎光临 深圳全飞鸿 (http://www.nagomes.com/disc/) |
Powered by Discuz! X3.2 |