我试图用JSON处理JSON结构。. NET中遇到了一些问题:
当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);
我不知道为什么我没有得到包含"测量"子数组数据的对象。
所以我挣扎了相当长的一段时间得到这个工作。然而,最终的解决方案并不是那么困难。希望我的回答能帮助到一些人。
我的解决方案是
- 使用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对象复制到输入框中,然后单击generate按钮。复制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; } }
然后在控制器中使用以下代码(只是复制了完整的文件)
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(); } } }
现在一切都可以正常工作了,数组中的数组等等。希望你发现我的解决方案有帮助,注意我不是一个顽固的程序员,所以如果我错过了效率的事情,我希望从你那里得到一些技巧来改进这段代码…
最诚挚的问候,雷蒙德
你就快成功了。只使用
var myObject = JsonConvert.DeserializeObject<CAKR.Graph.RootObject>(MyJsonString);
不是var myObject = JsonConvert.DeserializeObject<CAKR.Graph.Measurement>(MyJsonString);
首先,您确实不需要将Measurement
类的所有属性命名为与MyJsonString
中呈现的完全相同。你可以使用JsonProperty
属性来修饰你的类属性。
另一件事是,如果你想反序列化MyJsonString
的一部分,只提取Measurement
的数组,你应该为Deserialize
方法提供正确的T类型(在你的情况下是IEnumerable<Measurement>
)。
下面的代码应该有帮助:
dynamic context = JObject.Parse(MyJsonString);
var myObject = JsonConvert.DeserializeObject<IEnumerable<Measurement>>(context.measurement.ToString());
我使用一个非常简单的方法来反序列化Json数组。而不是使用大量带有大量公共变量的公共类。我只是使用一个动态对象,并将json作为对象传递给JSONConvert.DeserializeObject。
这就是它的工作原理。假设我有以下JSON:
string json = { 'Name': 'John Doe',
'Address': { 'City': 'Atlanta', 'State': 'GA' },
'Age': 30}
我可以将字符串json传递给JSONConvert.DeserializeObject。
dynamic outputArray = JsonConvert.DeserializeObject(json);
然后使用刚刚创建的动态项,我可以像这样收集Json数据。
string getName = outputArray.Name //This will return "John Doe"
如果Json中有数组,可以使用
string getCity = outputArray.Address.City; //This will return "Atlanta".
在没有公共变量集群的情况下,很容易改变从哪里提取数据…如果需要,您仍然可以将值保存到公共变量中。
下面是我如何使用完整的方法:
using (var client = new WebClient())
{
string json = client.DownloadString(url);
string output = json.ToString();
dynamic outputArray = JsonConvert.DeserializeObject(output);
string _age = outputArray.age;
string appID = outputArray.data.app_id;
Debug.Write(outputArray.Something); //Just match value of json
}