开发者

parsing json with jrock in vb.net

开发者 https://www.devze.com 2023-04-03 18:54 出处:网络
I am attempting to parse some json I am getting back from a service. For example it\'s returning { \"error\" :

I am attempting to parse some json I am getting back from a service. For example it's returning

      {
  "error" : 
  {
    "code" : 500, 
    "message" : "Error Executing Task. Error executing tool.", 
    "details" : []
  }
}

All I need is the text in the message field. Using jayrock what is the easity way to get to the data. I have:

 Dim request As HttpWebRequest
    Dim response As HttpWebResponse = Nothing
    Dim jarray As Jayrock.Json.JsonObject
request = DirectCast(WebRequest.Create(url), HttpWebRequest)
        response = DirectCast(request.GetResponse(), Htt开发者_开发百科pWebResponse)

        Dim reader As JsonTextReader = New JsonTextReader((New StreamReader(response.GetResponseStream)))


        jarray = Jayrock.Json.Conversion.JsonConvert.Import(reader)

what is the most efficant way of getting the "message" data


I can only speak for myself, but I'd write it this way:

Dim cMessage As String = "{ ""error"" : { ""code"" : 500, " & _
                                """message"" : ""Error Executing Task. " & _
                                "Error executing tool.""," & _
                                """details"" : [] " & _
                                "}" & _
                                "}"

Dim objResponse As JsonObject


objResponse = CType(JsonConvert.Import(cMessage), JsonObject)

MsgBox( "Last response was: " + objResponse("error")("message") )

P.S.: Don't forget to import Jayrock.Json and Jayrock.Json.Conversion

0

精彩评论

暂无评论...
验证码 换一张
取 消