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

时间:2015-12-19 20:51:28

标签: json parsing json.net

[
  ["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观众中时,他们似乎都在解析它。

1 个答案:

答案 0 :(得分:2)

您的JSON不代表对象;它是一个数组(数组)。因此,您无法使用[peripheral respondToRequest:[requests objectAtIndex:0] withResult:CBATTErrorSuccess]; CBATTRequest *aRequest = requests[0]; NSData *aData = aRequest.value; NSString *string = [[NSString alloc] initWithData:aData encoding:NSUTF8StringEncoding]; [self logToDelegate:string]; 。相反,使用} ,它可以处理数组,或使用 -(void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {,它可以处理数组或对象。



if(error) {
    [self callbackError:error];
    return;
}

[self logToDelegate:@"didWriteValueForCharacteristic"];

} //didWriteValueForCharacteristic

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