我有以下json字符串,并希望得到“消息”的值,它们都包含或多或少相同的格式。我通常的代码不起作用:
Dim jobj As JObject = JObject.Parse(data)
Dim skipFlag As Boolean = False
Dim token As JToken = JObject.Parse(data)
Dim results As List(Of JToken) = jobj.Children().ToList
For Each item As JProperty In results
item.CreateReader()
If item.Name = "error" Then
End If
Next
只返回一个值,不知道如何捕获值。
{"error":
{"message":
"Rule '\"CVS_Extra ' is invalid. mismatched character '<EOF>'
expecting '\"' (at position 21)\nno viable alternative at input '<EOF>'
\nRule '\"walgreens.com\" TX a prescription\" ' is invalid.
mismatched character '<EOF>' expecting '\"' (at position 45)
\nRule '\"walgreen's\" TX a prescription\" ' is invalid.
mismatched character '<EOF>' expecting '\"' (at position 42)
\nRule '\"cvs.com\" TX a prescription\" ' is invalid.
mismatched character '<EOF>' expecting '\"' (at position 39)
\nRule '\"C V S\" TX a prescription\" ' is invalid.
mismatched character '<EOF>' expecting '\"' (at position 37)
\nRule '\"H E Butt\" TX a prescription\" ' is invalid.
mismatched character '<EOF>' expecting '\"' (at position 40)
\nRule '\"H-E-B\" TX a prescription\" ' is invalid.
mismatched character '<EOF>' expecting '\"' (at position 37)
\nRule '\"HEB.com\" TX a prescription\" ' is invalid.
mismatched character '<EOF>' expecting '\"' (at position 39)
\n","sent":"2013-08-09T15:49:51+00:00"}}
答案 0 :(得分:1)
我可以用C#给你...但是在VB.NET中转换应该很容易。
JObject obj = JObject.Parse(json); // json == your json
JObject error = obj["error"] as JObject;
if (error != null)
{
JValue message = error["message"] as JValue;
if (message != null)
{
// found
}
}
在VB.NET中应该是:
Dim jobj As JObject = JObject.Parse(json) ' json is your json
Dim jerror As JObject = TryCast(jobj("error"), JObject)
If jerror IsNot Nothing Then
Dim jmessage As JValue = TryCast(jerror("message"), JValue)
If jmessage IsNot Nothing Then
Dim message As String = jmessage.Value(Of String)()
Dim lines As String() = message.Replace(vbCrLf, vbCr & vbCr).Split(New String()() = { vbLf }, StringSplitOptions.None).[Select](Function(p As String) p.Replace(vbCr & vbCr, vbCrLf)).ToArray(Of String)()
End If
End If
(更改了变量的名称,因为error
是VB.NET中的保留字)