2015-12-19 24 views
0
[ 
    ["Sender", "[email protected]"], 
    ["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", "<[email protected]>"], 
    ["Reply-To", "[email protected]"], 
    ["X-Mailgun-Skip-Verification", "false"], 
    ["To", "John Myers <[email protected]>"], ["From", "\"Inc.\" <[email protected]>"], 
    ["Subject", "Test subject"], 
    ["Mime-Version", "1.0"], 
    ["Content-Type", 
     ["multipart/mixed", { "boundary": "e43d638b70f04a40889d14f4c8422953" } ] 
    ] 
] 

當使用JObject(VB.net)來解析這個JSON字符串,JObject.parse引發此錯誤:如何使用JObject

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觀衆,他們似乎都解析得很好。

回答

2

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

string json = @" 
[ 
    [""Sender"", ""[email protected]""], 
    [""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"", ""<[email protected]>""], 
    [""Reply-To"", ""[email protected]""], 
    [""X-Mailgun-Skip-Verification"", ""false""], 
    [""To"", ""John Myers <[email protected]>""], 
    [""From"", ""\""Inc.\"" <[email protected]>""], 
    [""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