C# Json对象数组
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace JsonObjectArrayDemo
{
class Program
{
static void Main(string[] args)
{
List<JObject> jObjectList = new List<JObject>();
Dictionary<string, object> testItemDict;
for (int i = 1; i <= 2; i++)
{
testItemDict = new Dictionary<string, object>();
testItemDict.Add("testName", "testItem"+i.ToString());
testItemDict.Add("testValue", "11.99V");
testItemDict.Add("Maximum", "12.19V");
testItemDict.Add("Minimum", "11.89V");
testItemDict.Add("testResult", "OK");
jObjectList.Add(JObject.Parse(JsonConvert.SerializeObject(testItemDict)));
}
Object[] objectArray = jObjectList.ToArray();
Console.WriteLine(JsonConvert.SerializeObject(objectArray));
Console.ReadLine();
}
}
}
效果图
控制台输出的JObject数组
[{"testName":"testItem1","testValue":"11.99V","Maximum":"12.19V","Minimum":"11.89V","testResult":"OK"},{"testName":"testItem2","testValue":"11.99V","Maximum":"12.19V","Minimum":"11.89V","testResult":"OK"}]