小编And*_*tro的帖子

JSON Newtonsoft C# - 反序列化JSON文件中的特定字段

我正在使用一个巨大的JSON文件,只需要在其中提取一些字段.我一直在寻找一些反序列化的方法,但是不想在C#中使用JSON中的所有字段创建整个Class和Object,这将是很多无用的内存.

我可以使用Webclient获取JSON文件:

using (WebClient wc = new WebClient())
{
   jsonWeb = wc.DownloadString("http://link_to_get_JSON");
}

//Deserialize into a JObject
JObject obj = JObject.Parse(jsonWeb);

//Tried to access the info with
var val = obj.PropTwo;
var solution = obj.Descendants().OfType<JProperty>().Where(p => p.Name == "solverSolution").Select(x => x.Value.ToString()).ToArray();
Run Code Online (Sandbox Code Playgroud)

我真的找不到在JObject中获取所需字段的方法.在JSON内部,唯一需要的信息是solverSolution:{}如下:

{
   "content":
   [
      {
         "id":"f4d7e7f5-86ab-4155-8336-ca5f552cb3b4",
         "name":"m1",
         "description":"m1",
         "maxHeight":2000.0,
         "layers":6,
         "pallet":{},
         "product":{},
         "solverSolution":
         {
            "id":"106ef605-d95e-4c74-851b-63310fbcbc7d",
            "name":"solver",
            "maxHeight":2000.0,
            "layers":6,
            "solution":[
            {
               "X1":0,
               "Y1":0,
               "Z1":0,
               "X2":296,
               "Y2":246,
               "Z2":220
            },
            ...
            "default":false
         },
         "customSolutions":[0]
      },
     {}, …
Run Code Online (Sandbox Code Playgroud)

c# json json.net

1
推荐指数
1
解决办法
185
查看次数

标签 统计

c# ×1

json ×1

json.net ×1