0

我有以下 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"}}
4

1 回答 1

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 中的保留字)

于 2013-08-09T17:11:35.757 回答