• C# JArray.Add方法代码示例json数组添加数据方法


    https://vimsky.com/examples/detail/csharp-ex-Newtonsoft.Json.Linq-JArray-Add-method.html

     private void button1_Click(object sender, EventArgs e)
            {
                JArray jarray = new JArray();
    
    
                string pkgType = "";
                string pkgPart = "ABCDELMBT3906LT1G";
                string pkgLotId = "CDEGFLA68036148";
                string dateCode = "6134";
                string pkgQty = "6000";
                string pkSn = "XXXLA121420F9F0AA";
                string noNumber = "002";
                string currboxpkgsn = "LA1214218E6BAA";
    
                pkgPart = "CDELMBT3906LT1G";
                pkgLotId = "TT2LA18036148";
                dateCode = "8134";
                pkgQty = "6000";
                pkSn = "AATLA121420F9F1AA";
                noNumber = "003";
                currboxpkgsn = "AATLA1214218E7BAA";
    
    
    
                JArray relationships = new JArray();
                JObject relationShip1 = new JObject();
    
                relationShip1.Add("lotId",new JValue("ATLA68036148"));
                //条信息
                for (int i = 0; i < 10; i++)
                {
                    jarray.Add(new JObject()
                {
                    { "pkgType",pkgType},
                    { "pkgPart",pkgPart},
                    { "pkgLotId",pkgLotId},
                    { "dateCode",dateCode},
                    { "noNumber",(i+1).ToString()},
                    { "dateTime",System.DateTime.Now.ToString()}
    
                }
                   );
    
                }
                
                //盒信息
                pkgType = "";
                for (int i = 0; i < 1; i++)
                {
                    jarray.Add(new JObject()
                {
                    { "pkgType",pkgType},
                    { "pkgPart",pkgPart},
                    { "pkgLotId",pkgLotId},
                    { "dateCode",dateCode},
                    { "dateTime",System.DateTime.Now.ToString()}
    
                }
                   );
    
                }
                relationShip1.Add("acdedefLblCheckModelList", jarray);
    
    
    
    
    
    
    
    
    
                //字符串转json
                // dynamic jsonStr = JsonConvert.DeserializeObject<dynamic>(retString);
    
                //json转字符串
                string output = JsonConvert.SerializeObject(jarray, Newtonsoft.Json.Formatting.Indented);
    
                string output2 = JsonConvert.SerializeObject(relationships, Newtonsoft.Json.Formatting.Indented);
                string output3 = JsonConvert.SerializeObject(relationShip1, Newtonsoft.Json.Formatting.Indented);
    
    
            }
    {
      "lotId": "ATLA68036148",
      "acdedefLblCheckModelList": [
        {
          "pkgType": "",
          "pkgPart": "CDELMBT3906LT1G",
          "pkgLotId": "TT2LA18036148",
          "dateCode": "8134",
          "noNumber": "1",
          "dateTime": "2022/4/6 22:18:32"
        },
        {
          "pkgType": "",
          "pkgPart": "CDELMBT3906LT1G",
          "pkgLotId": "TT2LA18036148",
          "dateCode": "8134",
          "noNumber": "2",
          "dateTime": "2022/4/6 22:18:32"
        },
        {
          "pkgType": "",
          "pkgPart": "CDELMBT3906LT1G",
          "pkgLotId": "TT2LA18036148",
          "dateCode": "8134",
          "noNumber": "3",
          "dateTime": "2022/4/6 22:18:32"
        },
        {
          "pkgType": "",
          "pkgPart": "CDELMBT3906LT1G",
          "pkgLotId": "TT2LA18036148",
          "dateCode": "8134",
          "noNumber": "4",
          "dateTime": "2022/4/6 22:18:32"
        },
        {
          "pkgType": "",
          "pkgPart": "CDELMBT3906LT1G",
          "pkgLotId": "TT2LA18036148",
          "dateCode": "8134",
          "noNumber": "5",
          "dateTime": "2022/4/6 22:18:32"
        },
        {
          "pkgType": "",
          "pkgPart": "CDELMBT3906LT1G",
          "pkgLotId": "TT2LA18036148",
          "dateCode": "8134",
          "noNumber": "6",
          "dateTime": "2022/4/6 22:18:32"
        },
        {
          "pkgType": "",
          "pkgPart": "CDELMBT3906LT1G",
          "pkgLotId": "TT2LA18036148",
          "dateCode": "8134",
          "noNumber": "7",
          "dateTime": "2022/4/6 22:18:32"
        },
        {
          "pkgType": "",
          "pkgPart": "CDELMBT3906LT1G",
          "pkgLotId": "TT2LA18036148",
          "dateCode": "8134",
          "noNumber": "8",
          "dateTime": "2022/4/6 22:18:32"
        },
        {
          "pkgType": "",
          "pkgPart": "CDELMBT3906LT1G",
          "pkgLotId": "TT2LA18036148",
          "dateCode": "8134",
          "noNumber": "9",
          "dateTime": "2022/4/6 22:18:32"
        },
        {
          "pkgType": "",
          "pkgPart": "CDELMBT3906LT1G",
          "pkgLotId": "TT2LA18036148",
          "dateCode": "8134",
          "noNumber": "10",
          "dateTime": "2022/4/6 22:18:32"
        },
        {
          "pkgType": "",
          "pkgPart": "CDELMBT3906LT1G",
          "pkgLotId": "TT2LA18036148",
          "dateCode": "8134",
          "dateTime": "2022/4/6 22:18:32"
        }
      ]
    }
    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace Json数组添加数据
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                JArray jarray = new JArray();
                jarray.Add(new JObject()
                {
                    { "name","scott"},
                    { "age",100},
                    { "country","China"},
                    { "time","null"}
    
                }
                    );
                jarray.Add(new JObject()
                {
                    { "name","scott"},
                    { "age",100},
                    { "country","China"},
                    { "time",System.DateTime.Now.ToString()}
    
                }
                   );
    
                //字符串转json
                // dynamic jsonStr = JsonConvert.DeserializeObject<dynamic>(retString);
    
                //json转字符串
                string output = JsonConvert.SerializeObject(jarray, Newtonsoft.Json.Formatting.Indented);
    
              
            }
        }
    }
      public void GetColumnsFromItemsFallthroughNullTest()
            {
                JArray array = new JArray();
                array.Add(new JObject()
                {
                    { "integer", 1 },
                    { "value1", "string"},
                    { "boolean", true },
                    { "null", null }
    
                });
                array.Add(new JObject()
                {
                    { "integer", 1 },
                    { "value1", "string"},
                    { "boolean", true },
                    { "null", DateTime.Now }
                });
    
                IDictionary<string, Column> cols = storage.GetColumnsFromItems(array);
    
                Assert.AreEqual(4, cols.Count);
    
                Assert.IsTrue(cols.ContainsKey("integer"));
                // json.net makes int long
                Assert.AreEqual(typeof(long), cols["integer"].GetClrDataType());
                Assert.IsTrue(cols.ContainsKey("value1"));
                Assert.AreEqual(typeof(string), cols["value1"].GetClrDataType());
                Assert.IsTrue(cols.ContainsKey("boolean"));
                Assert.AreEqual(typeof(bool), cols["boolean"].GetClrDataType());
                Assert.IsTrue(cols.ContainsKey("null"));
                Assert.AreEqual(typeof(DateTime), cols["null"].GetClrDataType());
            }
  • 相关阅读:
    SharePoint 2016 图文安装教程
    CSS选择器的特殊性和LOVE HA
    CSS相邻兄弟选择器
    Javascript高级程序设计——函数
    Javascript高级程序设计——基本概念(二)
    Javascript高级程序设计——基本概念(一)
    Javascript高级程序设计——在HTML中使用Javascript
    Javascript高级程序设计——javascript简介
    Javascript包含对象的数组去重
    JQuery阻止表单提交的方法总结
  • 原文地址:https://www.cnblogs.com/txwtech/p/16109578.html
Copyright © 2020-2023  润新知