簡體   English   中英

Newtonsoft.Json - 將 JSON 反序列化為自定義 JObject

[英]Newtonsoft.Json - Deserialize JSON to a custom JObject

我從 API 得到了這個結構。

[
  {
    "Id": 611932,
    "ProjectId": 6079,
    "Title": "Issue1"
    "Viewpoints": [
      {
        "Id": 885758,
        "IssueId": 611932,
        "Pinpoint": {
          "Position": {
            "X": -32.452857971191406,
            "Y": -14.971426010131836,
            "Z": 9.1110143661499023
          }
        }
      },
      {
        "Id": 885764,
        "IssueId": 611932,
        "Pinpoint": {
          "Position": {
            "X": -21.042057037353516,
            "Y": -21.742080688476563,
            "Z": 7.72857141494751
          }
        }
      }
    ]
  },
  {
    "Id": 611931,
    "ProjectId": 6079,
    "Title": "Issue2",
    "Viewpoints": [
      {
        "Id": 885757,
        "IssueId": 611931
      },
      {
        "Id": 885762,
        "IssueId": 611931,
        "Pinpoint": {
          "Position": {
            "X": -29.135581970214844,
            "Y": -14.971452713012695,
            "Z": 7.0672616958618164
          }
        }
      }
    ]
  },
  {
    "Id": 611930,
    "ProjectId": 6079,
    "Title": "Issue3",
    "Viewpoints": [
      {
        "Id": 885756,
        "IssueId": 611930,
      }
    ]
  }
]

我希望能夠以對象的形式構造一個包含所有“視點”中的所有“Pinpoint”的新結構,因此生成的結構看起來像這樣(它不必在一個數組中,它也可以包含為一個新對象):

[
  {
    "Id": 885758,
    "IssueId": 611932,
    "Pinpoint": {
      "Position": {
        "X": -32.452857971191406,
        "Y": -14.971426010131836,
        "Z": 9.1110143661499023
      }
    }
  },
  {
    "Id": 885764,
    "IssueId": 611932,
    "Pinpoint": {
      "Position": {
        "X": -21.042057037353516,
        "Y": -21.742080688476563,
        "Z": 7.72857141494751
      }
    }
  },
  {
    "Id": 885762,
    "IssueId": 611931,
    "Pinpoint": {
      "Position": {
        "X": -29.135581970214844,
        "Y": -14.971452713012695,
        "Z": 7.0672616958618164
      }
    }
  }
]

請注意,我沒有包含來自 Issue3 的“Viewpoint”數組和來自 issue2 的“Viewpoint”數組的一部分,因為它們不包含“Pinpoint”......

關於如何實現這一目標的任何想法? 到目前為止,我已經嘗試將每個“位置”對象添加到列表中,僅用於測試目的:

var jArray = JsonConvert.DeserializeObject<dynamic>(response.Content);

List<JToken> results = new List<JToken>();

foreach (JObject jObject in jArray)
{
    results.Add(new JObject(jObject["Viewpoints"].Children()["Pinpoint"]["Position"]));
}

但是我收到此錯誤:

無法將 Newtonsoft.Json.Linq.JObject 添加到 Newtonsoft.Json.Linq.JObject。

使用System.Linq

using System.Linq;

解決方案 1

  1. jObject["Viewpoints"]數組中添加JObject元素的范圍。
  2. 和 。 Where()過濾 JObject 包含“Pinpoint”屬性。
foreach (JObject jObject in jArray)
{   
    results.AddRange(jObject["Viewpoints"].Children());
}
        
        
results = results
    .Where(x => x["Pinpoint"] != null)
    .ToList();

僅獲取“位置”對象:

results = results
    .Where(x => x["Pinpoint"] != null && x["Pinpoint"]["Position"] != null)
    .Select(x => x["Pinpoint"]["Position"])
    .ToList();

解決方案 2

此解決方案的工作原理與解決方案 1相同。

List<JToken> results = JArray.Parse(response.Content)
    .SelectMany(x => x["Viewpoints"])
    .Where(x => x["Pinpoint"] != null)
    .ToList();

僅獲取“位置”對象:

List<JToken> results = JArray.Parse(json)
    .SelectMany(x => x["Viewpoints"])
    .Where(x => x["Pinpoint"] != null && x["Pinpoint"]["Position"] != null)
    .Select(x => x["Pinpoint"]["Position"])
    .ToList();

.NET 小提琴示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM