请参阅:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.IO;
namespace TestJson2
{
class Program
{
private static List<string> myCollections;
static void Main(string[] args)
{
myCollections = new List<string>();
myCollections.Add("frog");
myCollections.Add("dog");
myCollections.Add("cat");
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
using (JsonWriter jsonWriter = new JsonTextWriter(sw))
{
jsonWriter.Formatting = Formatting.None;
jsonWriter.WriteStartObject();
jsonWriter.WritePropertyName("id");
jsonWriter.WriteValue("12345");
jsonWriter.WritePropertyName("title");
jsonWriter.WriteValue("foo");
string animals = CollectionToJson();
jsonWriter.WritePropertyName("animals");
jsonWriter.WriteValue(animals);
jsonWriter.WriteEndObject();
}
var result = sw.ToString();
}
private static string CollectionToJson()
{ …Run Code Online (Sandbox Code Playgroud)