我正在尝试使用 Json.NET 处理 JSON 结构并遇到一些问题:
当 JSON 包含未命名数组时,我的类无法工作。
Json 示例:
{
"graph_property" : [{
"name" : "calculation_method",
"value" : "Arithmetic"
}, {
"name" : "graph_type",
"value" : "TIME"
}
],
"measurement" : [{
"id" : "9997666",
"alias" : "Measurement (TxP)[IE]-Home Page - Total Time (seconds)",
"bucket_data" : [{
"name" : "2013-MAR-18 12:00 AM",
"id" : 1,
"perf_data" : {
"value" : "2.244",
"unit" : "seconds"
},
"avail_data" : {
"value" : "99.67",
"unit" : "percent"
},
"data_count" : {
"value" : "299",
"unit" : "#"
}
}
],
"graph_option" : [{
"name" : "perfwarning",
"value" : "-",
"unit" : "seconds"
}, {
"name" : "perfcritical",
"value" : "-",
"unit" : "seconds"
}, {
"name" : "availwarning",
"value" : "-",
"unit" : "percent"
}, {
"name" : "availcritical",
"value" : "-",
"unit" : "percent"
}, {
"name" : "bucketsize",
"value" : "86400",
"unit" : "seconds"
}, {
"name" : "rows",
"value" : "1",
"unit" : "#"
}, {
"name" : "pagecomponent",
"value" : "Total Time",
"unit" : "seconds"
}, {
"name" : "avg_perf",
"value" : "2.244",
"unit" : "seconds"
}, {
"name" : "avg_avail",
"value" : "99.67",
"unit" : "percent"
}, {
"name" : "total_datapoint_count",
"value" : "300",
"unit" : "#"
}, {}
]
}, {
"id" : "9997666",
"alias" : "Measurement (TxP)[IE]-Women - Total Time (seconds)",
"bucket_data" : [{
"name" : "2013-MAR-18 12:00 AM",
"id" : 1,
"perf_data" : {
"value" : "0.979",
"unit" : "seconds"
},
"avail_data" : {
"value" : "100.00",
"unit" : "percent"
},
"data_count" : {
"value" : "299",
"unit" : "#"
}
}
],
"graph_option" : [{
"name" : "perfwarning",
"value" : "-",
"unit" : "seconds"
}, {
"name" : "perfcritical",
"value" : "-",
"unit" : "seconds"
}, {
"name" : "availwarning",
"value" : "-",
"unit" : "percent"
}, {
"name" : "availcritical",
"value" : "-",
"unit" : "percent"
}, {
"name" : "bucketsize",
"value" : "86400",
"unit" : "seconds"
}, {
"name" : "rows",
"value" : "1",
"unit" : "#"
}, {
"name" : "pagecomponent",
"value" : "Total Time",
"unit" : "seconds"
}, {
"name" : "avg_perf",
"value" : "0.979",
"unit" : "seconds"
}, {
"name" : "avg_avail",
"value" : "100.00",
"unit" : "percent"
}, {
"name" : "total_datapoint_count",
"value" : "299",
"unit" : "#"
}, {}
]
}
],
"link" : {
"type" : "application/json",
"href" : "http://api.website.tld?format=json",
"rel" : "slotmetadata"
}
}
Json.NET 类:
using System;
using System.Collections.Generic;
namespace CAKR.Graph
{
/// <summary>
/// Description of KN_Graph.
/// </summary>
public class GraphProperty
{
public string name { get; set; }
public string value { get; set; }
}
public class PerfData
{
public string value { get; set; }
public string unit { get; set; }
}
public class AvailData
{
public string value { get; set; }
public string unit { get; set; }
}
public class DataCount
{
public string value { get; set; }
public string unit { get; set; }
}
public class BucketData
{
public string name { get; set; }
public int id { get; set; }
public PerfData perf_data { get; set; }
public AvailData avail_data { get; set; }
public DataCount data_count { get; set; }
}
public class GraphOption
{
public string name { get; set; }
public string value { get; set; }
public string unit { get; set; }
}
public class Measurement
{
public string id { get; set; }
public string alias { get; set; }
public List<BucketData> bucket_data { get; set; }
public List<GraphOption> graph_option { get; set; }
}
public class Link
{
public string type { get; set; }
public string href { get; set; }
public string rel { get; set; }
}
public class RootObject
{
public List<GraphProperty> graph_property { get; set; }
public List<Measurement> measurement { get; set; }
public Link link { get; set; }
}
}
我的代码:
var myObject = JsonConvert.DeserializeObject<CAKR.Graph.Measurement>(MyJsonString);
我不确定为什么我没有得到包含“Measurment”子数组数据的对象。 如果我插入命名值,它会起作用...
最佳答案
所以我花了很长一段时间才让这个工作正常进行。然而最终解决方案并没有那么困难。希望我的回答能帮助一些人。
我的解决方案是
- 使用 nu.get 将 JSON.net 安装到您的项目中
- 使您的 JSON 对象包括数组中的数组等。确保对象的格式正确!例子...
{"ProductDetail":[
{ "ProjectImg" : "http://placehold.it/400x300", "Category" : "Cars", "ProjectTitle" : "Cars of the future", "ProjectDesc" : "Test project", "GenSpecList" : ["Specs1", "Specs2", "Specs3", "Specs4"], "OptionList" : [{ "OptionNr" : "1", "Options" : ["Opt1", "Opt2", "Opt3"] }, { "OptionNr" : "2", "Options" : ["Opt1", "Opt2", "Opt3"] }, { "OptionNr" : "3", "Options" : ["Opt1", "Opt2", "Opt3"] }, { "OptionNr" : "4", "Options" : ["Opt1", "Opt2", "Opt3"] }, { "OptionNr" : "5", "Options" : ["Opt1", "Opt2", "Opt3"] }, { "OptionNr" : "6", "Options" : ["Opt1", "Opt2", "Opt3"] } ], "Articles" : [{ "tileImg" : "'Images/Project/1.jpg'", "tileTit" : "Title1", "tileArt" : "Article text here..." }, { "tileImg" : "'Images/Project/2.jpg'", "tileTit" : "Title2", "tileArt" : "Article text here..." }, { "tileImg" : "'Images/Project/3.jpg'", "tileTit" : "Title3", "tileArt" : "Article text here..." }, { "tileImg" : "'Images/Project/4.jpg'", "tileTit" : "Title4", "tileArt" : "Article text here..." } ] } ] }
- 转到 json2csharp.com 并将您的 JSON 对象复制到输入框中,然后单击生成按钮。将生成的 csharp 模型(这实际上是解决我的难题的关键!)复制到您的 ViewModel 中。
在我的例子中,json2csharp.com 生成的所有类的主要类是 RootObject,如下所示
public class Article { public string tileImg { get; set; } public string tileTit { get; set; } public string tileArt { get; set; } } public class OptionList { public string OptionNr { get; set; } public List<string> Options { get; set; } } public class ProductDetail { public string ProjectImg { get; set; } public string Category { get; set; } public string ProjectTitle { get; set; } public string ProjectDesc { get; set; } public List<string> GenSpecList { get; set; } public List<OptionList> OptionList { get; set; } public List<Article> Articles { get; set; } } public class RootObject { public List<ProductDetail> ProductDetail { get; set; } }
然后在controller中使用下面的代码(这里只是复制了完整的文件)
using Project.Details; //<-- this is my ViewModel namespace name using Newtonsoft.Json; using System.IO; using System.Threading.Tasks; namespace WebApplication.Controllers { public class JSONController : Controller { // // GET: /JSON/ public async Task<ActionResult> Index() { StreamReader file = new StreamReader("C:\\Users\\YourName\\etc\\File.json"); String json = await file.ReadToEndAsync(); var Project = JsonConvert.DeserializeObject<RootObject>(json); return View(); } } }
现在一切正常,数组在数组中等等。 希望我的解决方案对您有所帮助,注意我不是顽固的程序员,所以如果我错过了关于效率的事情,我希望能收到您的一些提示以改进此代码...
最好的问候, 雷蒙德
关于c# - Json.NET:反序列化 json 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15507088/