CSharp · Console
using Newtonsoft.Json.Linq;
using System;
using System.Json;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
// create the json text here
Console.WriteLine("create the json text here");
var json = JToken.FromObject(new Rootobject_Labresult
{
labresults = Enumerable.Range(1, 10).Select(i =>
new Labresult {
createddate = DateTime.Today.ToString("yyyyMMdd"),
createddatetime = DateTime.Now,
departmentid = i.ToString(),
description = string.Format("Item {0}", i)
}
).ToArray(),
totalcount = 10
}).ToString(0); // 0 = Newtonsoft.Json.Formatting.None
Console.WriteLine(json);
Console.WriteLine();
// the JsonValue you get from API
JsonValue jsonVal = JsonValue.Parse(json);
Console.WriteLine("the JsonValue you get from API");
Console.WriteLine(jsonVal.ToString());
Console.WriteLine();
// Convert from JsonValue to Rootobject_Labresult
Rootobject_Labresult result = JToken.Parse(jsonVal.ToString()).ToObject<Rootobject_Labresult>();
// Print the object out
Console.WriteLine("Print the object out");
Console.WriteLine(JToken.FromObject(result));
Console.WriteLine();
}
}
public class Rootobject_Labresult
{
public Labresult[] labresults { get; set; }
public int totalcount { get; set; }
}
public class Labresult
{
public string createddate { get; set; }
public DateTime createddatetime { get; set; }
public string departmentid { get; set; }
public string description { get; set; }
}