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()); }