none
HttpWebRequest Parse JSON object array value RRS feed

  • Question

  • Trying to access the attributes of an HttpWebResponse and able to get "statuscode" and "errormessage" but I can't seem to access the "duplicate" value found in this response:

    {"statuscode":"0","errorcode":"10","errormessage":"Already Registered Tools : MOBILEPHONE","duplicate":[["293954372"]]}

    Here's what I have so far:

    Dim postData As String = "&request_id=" & request_id
    
            Dim myRequest2 As HttpWebRequest = DirectCast(WebRequest.Create("http://somewebsite/checkresponse"), HttpWebRequest)
            Dim data As Byte() = System.Text.Encoding.UTF8.GetBytes(postData)
    
            myRequest2.Method = "POST"
            myRequest2.ContentType = "application/x-www-form-urlencoded"
            myRequest2.ContentLength = data.Length
            Dim newStream2 As Stream = myRequest2.GetRequestStream()
            ' Send the data.
            newStream2.Write(data, 0, data.Length)
            newStream2.Close()
    
            Dim webStream2 As Stream
            Dim webResponse2 = ""
            Dim res2 As HttpWebResponse
            res2 = CType(myRequest2.GetResponse(), HttpWebResponse) ' Send Request
            webStream2 = res2.GetResponseStream() ' Get Response
            Dim webStreamReader2 As New StreamReader(webStream2)
            While webStreamReader2.Peek >= 0
                webResponse2 = webStreamReader2.ReadToEnd()
            End While
            webStream2.Close()
    
            Dim json As Newtonsoft.Json.Linq.JObject = JObject.Parse(webResponse2.ToString)
    
            Dim errorcode As String = json.SelectToken("errormessage")
            Dim statuscode As String = json.SelectToken("statuscode")
    
            'this seems to fail
            Dim dupvalue As String = json.SelectToken("duplicate")



    James Crandall ~ http://javitechnologies.com Spatial Database Solutions

    Tuesday, May 1, 2018 10:54 PM

Answers

  • It looks like the value is an array of an array, not a single string like the other property values.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by jamesfreddyc Wednesday, May 2, 2018 4:02 AM
    Wednesday, May 2, 2018 12:34 AM
    Moderator

All replies

  • It looks like the value is an array of an array, not a single string like the other property values.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by jamesfreddyc Wednesday, May 2, 2018 4:02 AM
    Wednesday, May 2, 2018 12:34 AM
    Moderator
  • Working from a Json string.

    Public Class Response1
        <JsonProperty("statuscode")>
        Public Property Statuscode As String
        <JsonProperty("errorcode")>
        Public Property Errorcode As String
        <JsonProperty("errormessage")>
        Public Property Errormessage As String
        <JsonProperty("duplicate")>
        Public Property Duplicate As String()()
    End Class

    Get elements

    Dim jsonString = <Text>{"statuscode":"0","errorcode":"10","errormessage":"Already Registered Tools : MOBILEPHONE","duplicate":[["293954372"]]}</Text>.Value
    Dim result As Response1 = JsonConvert.DeserializeObject(Of Response1)(jsonString)
    Console.WriteLine($"Duplicate: {result.Duplicate(0)(0)}")
    Console.WriteLine($"Statuscode: {result.Statuscode}")
    Console.WriteLine($"Errorcode: {result.Errorcode}")
    Console.WriteLine($"Errormessage: {result.Errormessage}")


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, May 2, 2018 12:35 AM
    Moderator
  • Hi Karen,

    Is there any way to reduce the reliance on another class like you've suggested?  This is code behind of an aspx page.


    James Crandall ~ http://javitechnologies.com Spatial Database Solutions


    Wednesday, May 2, 2018 12:57 AM
  • Hi Karen,

    Is there any way to reduce the reliance on another class like you've suggested?  This is code behind of an aspx page.


    James Crandall ~ http://javitechnologies.com Spatial Database Solutions


    I tend to go on the simply side when providing solutions. What I provided comes straight from the source.

    https://www.newtonsoft.com/json/help/html/DeserializeObject.htm

    DeserializeObject takes a type to obtain the elements you want which is strong typing via a concrete class.

    Simple is good but how you define simply is another thing so in my mind I've provided simple.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, May 2, 2018 1:35 AM
    Moderator
  • The token returned will be an array with another child array inside.  You can get the string values from that inner array.

    https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JToken.htm


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, May 2, 2018 1:36 AM
    Moderator
  • Yes, it's simple enough but was hoping to not have to create a whole other public class to access a single array in a json response!

    James Crandall ~ http://javitechnologies.com Spatial Database Solutions

    Wednesday, May 2, 2018 1:44 AM
  • Yes, it's simple enough but was hoping to not have to create a whole other public class to access a single array in a json response!

    James Crandall ~ http://javitechnologies.com Spatial Database Solutions

    Does not have to be Public and it cost you nothing to create a class. 

    Friend Class Response1
        Public Property Statuscode As String
        Public Property Errorcode As String
        Public Property Errormessage As String
        Public Property Duplicate As String()()
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, May 2, 2018 1:51 AM
    Moderator
  • It's wanting a Get and Set on the property example you've provided but I'm not sure if that's how you're intending it?

    Imports Newtonsoft.Json
    Imports Newtonsoft.Json.Linq
    Friend Class Response1
        Public Property Duplicate() As String()()
            Get
    
            End Get
            Set(ByVal value As String()())
    
            End Set
        End Property
    End Class
    
    Partial Class trial_page
        Inherits System.Web.UI.Page
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
    ...
    
    End Sub


    James Crandall ~ http://javitechnologies.com Spatial Database Solutions


    Wednesday, May 2, 2018 2:09 AM
  • See the following, it's good to read the docs :-)

    https://www.newtonsoft.com/json/help/html/SerializeObject.htm

    BTW this is what I consider ugly and unsafe by not using a class.

    Dim o = JObject.Parse(jsonString)
    Dim test = CType(CType(o.Last, JProperty).
            Value, JArray)(0)(0).
            Value(Of String)


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, May 2, 2018 2:22 AM
    Moderator
  • Karen -- I don't think that example will even compile.  It's already complaining before I can even run it. 

    James Crandall ~ http://javitechnologies.com Spatial Database Solutions

    Wednesday, May 2, 2018 2:43 AM
  • Since the "array" is always going to be a single value (guaranteed), I can simply access it as a string like so:

    Dim dup As String = json.SelectToken("duplicate[0]").ToString


    James Crandall ~ http://javitechnologies.com Spatial Database Solutions

    Wednesday, May 2, 2018 4:02 AM
  • I used your comment to find a solution.  Thanks!

    James Crandall ~ http://javitechnologies.com Spatial Database Solutions

    Wednesday, May 2, 2018 4:03 AM
  • Compiles fine with VS2017, if using a earlier version put all code form test onto one line.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, May 2, 2018 9:37 AM
    Moderator