c# 生成json字符串和解析json字符串处理
2021/7/22 12:36:30
本文主要是介绍c# 生成json字符串和解析json字符串处理,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
一.c# 生成json字符串
1.1 datatable生成json字符串
[HttpGet] [Route("api/QueryPlasmaInfo")] public HttpResponseMessage QueryPlasmaInfo(string project) { project = project.Trim(); JsonData jd = new JsonData(); DataTable dt = checkInplanservice.GetPlasmaInfoTable(project); //OBJ转化成JSON //var datajson = JsonConvert.SerializeObject(new{employees=data}); //返回实体类json数 //写法1 string json = JsonConvert.SerializeObject(dt); var datajson = JsonConvert.SerializeObject(new { Pro=project, PlasmaInfo = json }); //返回匿名类json数 //写法2 var v = new { Pro = project, PlasmaInfo = json }; var datajson2 = JsonConvert.SerializeObject(v); return new HttpResponseMessage() { Content = new StringContent(datajson + datajson2, Encoding.UTF8, "application/json"), }; }View Code
结果:
{"Pro":"W10IU80AP2-00","PlasmaInfo":"[{\"MiProject\":\"W10IU80AP2-00\",\"FgProject\":\"W10IU80AP2\",\"MiCustName\":\"Foxconn Tianjin(FOXCONN ODM)\",\"floors\":\"10\",\"MiConfirmDate\":\"2021-07-01T17:55:04\",\"vdpn\":\"IT-170GRA1TC\"}]"}{"Pro":"W10IU80AP2-00","PlasmaInfo":"[{\"MiProject\":\"W10IU80AP2-00\",\"FgProject\":\"W10IU80AP2\",\"MiCustName\":\"Foxconn Tianjin(FOXCONN ODM)\",\"floors\":\"10\",\"MiConfirmDate\":\"2021-07-01T17:55:04\",\"vdpn\":\"IT-170GRA1TC\"}]"}View Code
二.C#解析json数据
[HttpPost] [Route("api/GetLotCardMIMAFInfo")] public HttpResponseMessage GetLotCardMIMAFInfo() { var data = Request.Content.ReadAsStringAsync().Result; var PNlist = ""; JObject jsonObj = JObject.Parse(data); string PNno = jsonObj["PN"].ToString(); JArray jsonAry = JArray.Parse(jsonObj["cuPNList"].ToString()); JObject stu1Obj = JObject.Parse(jsonAry[0].ToString()); string cuPN = stu1Obj["cuPN"].ToString(); foreach (var ss in jsonAry) //查找某个字段与值 { PNlist = PNlist + ss["cuPN"].ToString() + ","; // ((JObject)ss)["cuPN"]; // JObject.Parse(jsonAry[0].ToString()); } JsonData jd = new JsonData(); DataTable dt = checkInplanservice.GettLotCardInfoTable(PNno); DataTable dtMAF = checkInplanservice.GettLotCardMAFInfoTable(PNlist); //OBJ转化成JSON jd.Status = HandleStatus.Success; if (dt.Rows.Count == 0) { jd.Status = HandleStatus.Fail; jd.Msg = "无数据"; } else { jd.Data = new { cuLayerType = dt.Rows[0]["LayerLevel"], cuCustomer = dt.Rows[0]["MiCustPn"], cuCustomerName = dt.Rows[0]["MiCustName"], cuQRCustomerPN = "", cuQRCustomerPNVersion = "", cuPNLOrigin = dt.Rows[0]["lvdr"], cuSize = dt.Rows[0]["Value"], cuCurrSize = dt.Rows[0]["MiSetArea"], cuHSFRequirement = dt.Rows[0]["AttrCode"], cuFinalThickness = dt.Rows[0]["cuFinalThickness"], cuPNList = dtMAF }; } string msgAll = JsonConvert.SerializeObject(jd); return JsonResult(msgAll); }View Code
这篇关于c# 生成json字符串和解析json字符串处理的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#
- 2024-01-24Advanced .Net Debugging 1:你必须知道的调试工具
- 2024-01-24.NET集成IdGenerator生成分布式全局唯一ID
- 2024-01-23用CI/CD工具Vela部署Elasticsearch + C# 如何使用