簡體   English   中英

如何使用JObject解析此Json字符串

[英]How to Parse this Json String using JObject

[
  ["Sender", "service@mydomain.com"], 
  ["Date", "Sat, 19 Dec 201520:41:31 +0000"], 
  ["X-Mailgun-Sid", "WyI0ZjRjNyIsICJyYWplZXZrbXh4eddHh4eDMzMzMzQHlhaG9vLmNvbSIsICJjNGExZiJd"],
  ["Received", "by luna.mailgun.net with HTTP; Sat, 19 Dec 2015 20:41:31+0000"], 
  ["Message-Id", "<201512192024131.73374.12565@mydomain.com>"], 
  ["Reply-To", "junky01@hotmail.com"], 
  ["X-Mailgun-Skip-Verification", "false"], 
  ["To", "John Myers <testxxxxxx33333@yahoo.com>"], ["From", "\"Inc.\" <service@mydomain.com>"], 
  ["Subject", "Test subject"],
  ["Mime-Version", "1.0"], 
  ["Content-Type", 
      ["multipart/mixed", { "boundary": "e43d638b70f04a40889d14f4c8422953" } ]
  ]
]

使用JObject(VB.net)時,JObject.parse引發此錯誤:

Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 2, position 2.---- at Newtonsoft.Json.Linq.JObject.Load(JsonReader reader) at Newtonsoft.Json.Linq.JObject.Parse(String json)

但是,當我將上述字符串復制到在線Json查看器中時,他們似乎都在解析它。

您的JSON不代表對象; 它是一個(數組)數組。 因此,您不能使用JObject.Parse() 相反,使用JArray.Parse()它可以處理陣列,或使用JToken.Parse()它可以處理任一陣列或對象。

string json = @"
[
  [""Sender"", ""service@mydomain.com""], 
  [""Date"", ""Sat, 19 Dec 201520:41:31 +0000""], 
  [""X-Mailgun-Sid"", ""WyI0ZjRjNyIsICJyYWplZXZrbXh4eddHh4eDMzMzMzQHlhaG9vLmNvbSIsICJjNGExZiJd""],
  [""Received"", ""by luna.mailgun.net with HTTP; Sat, 19 Dec 2015 20:41:31+0000""], 
  [""Message-Id"", ""<201512192024131.73374.12565@mydomain.com>""], 
  [""Reply-To"", ""junky01@hotmail.com""], 
  [""X-Mailgun-Skip-Verification"", ""false""], 
  [""To"", ""John Myers <testxxxxxx33333@yahoo.com>""], 
  [""From"", ""\""Inc.\"" <service@mydomain.com>""], 
  [""Subject"", ""Test subject""],
  [""Mime-Version"", ""1.0""], 
  [""Content-Type"", 
      [""multipart/mixed"", { ""boundary"": ""e43d638b70f04a40889d14f4c8422953"" } ]
  ]
]";

JArray rows = JArray.Parse(json);
foreach (JArray row in rows)
{
    Console.WriteLine(string.Join(": ", row.Select(r => r.ToString())));
}

小提琴: https : //dotnetfiddle.net/VRs47S

暫無
暫無

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

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