Announcement

Collapse
No announcement yet.

Json Deserialize (what do i forget) ?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Json Deserialize (what do i forget) ?

    Hi all,

    Solved at post reply.
    Thx



    I did write a script that let me grab some json data, but there is one thing i dont know why it won't want to work.
    When i enter "lat" it works but not "house_number"


    This is the json site

    Code:
    {"place_id":"164848013","licence":"API © LocationIQ.org CC BY 4.0, Data © testers, ODbL 1.0","osm_type":"way","osm_id":"437411681","lat":"17.42122785","lon":"78.4006673","display_name":"128, Road No 15, Prashasan Nagar, Hyderabad, Telangana, 500033, India","address":{"house_number":"128","road":"Road No 15","suburb":"Prashasan Nagar","city":"Hyderabad","state_district":"Hyderabad","state":"Telangana","postcode":"500033","country":"India","country_code":"in"},"boundingbox":["17.421132","17.4213237","78.4005923","78.4007423"]}
    This is my script :

    Code:
    Imports Newtonsoft.Json
    		Imports Newtonsoft.Json.Linq
    		Sub Main (params as Object)
    	Try	
    		Dim DataStr As String = hs.GetURL("http://test.org","/v1/reverse.php?format=json&key=mykey&lat=17.421223&lon=78.400674",true,80)  
            Dim obj As New Object
    		
    		Dim JSON As String = DataStr
           
    		dim json2 as string
    		JSON2 = JSON.replace("""","")
    		JSON2 = JSON.replace("©","")
    		Dim SplitStr() As String = JSON.Split(",") 
            Dim CurrentString As String 
            Dim DPosition As Integer 
            Dim StringToWorkWith As String
    		
    		
    		obj = JsonConvert.DeserializeObject(json2)
    		
    		hs.WriteLog("Value of device", obj.item("lat").Tostring)
    		
    		
        Catch ex As Exception
            hs.writelog("JSON", "Exception: " & ex.message)
    	End Try
    			
    		
    		end sub
    If i use this hs.WriteLog("Value of device", obj.item("lat").Tostring) it writes out the latitude perfectly.


    but when i wat to use hs.WriteLog("Value of device", obj.item("house_number").Tostring) it writes an error in homeseer :

    Code:
    JSON	Exception: Object reference not set to an instance of an object.
    Did i forget something ?

    regards
    Last edited by Malosa; September 29, 2017, 02:49 PM. Reason: Solved
    Preferred -> Jon's Plugins, Pushover, Phlocation, Easy-trigger,
    Rfxcom, Blade Plugins, Pushbullet, homekit, Malosa Scripts




    HS3Pro 4.1.14.0 on windows 10 enterprise X64 on hp quadcore laptop 8 GB.

    #2
    Yes, you did forget something.
    This is how JSON looks when you format it for human reading
    Code:
    {
         "place_id": 164848013,
         "licence": "API © LocationIQ.org CC BY 4.0, Data © testers, ODbL 1.0",
         "osm_type": "way",
         "osm_id": 437411681,
         "lat": 17.42122785,
         "lon": 78.4006673,
         "display_name": "128, Road No 15, Prashasan Nagar, Hyderabad, Telangana, 500033, India",
         "address": {
             "house_number": 128,
             "road": "Road No 15",
             "suburb": "Prashasan Nagar",
             "city": "Hyderabad",
             "state_district": "Hyderabad",
             "state": "Telangana",
             "postcode": 500033,
             "country": "India",
             "country_code": "in"
        },
         "boundingbox": [
            17.421132,
            17.4213237,
            78.4005923,
            78.4007423
        ]
    }
    As you can see "lat" is field in the top level of this JSON object. "house_number" is not. You first have to fetch "address", and then with its reference you cab fetch "house_number". So something like, obj.item("address").item("house_number").ToString

    Comment


      #3
      Solved

      Thanks for the help!!,

      I did to this hs.WriteLog("Value of device", obj.item("address")("house_number").Tostring) and it works




      Originally posted by crab987 View Post
      Yes, you did forget something.
      This is how JSON looks when you format it for human reading
      Code:
      {
           "place_id": 164848013,
           "licence": "API © LocationIQ.org CC BY 4.0, Data © testers, ODbL 1.0",
           "osm_type": "way",
           "osm_id": 437411681,
           "lat": 17.42122785,
           "lon": 78.4006673,
           "display_name": "128, Road No 15, Prashasan Nagar, Hyderabad, Telangana, 500033, India",
           "address": {
               "house_number": 128,
               "road": "Road No 15",
               "suburb": "Prashasan Nagar",
               "city": "Hyderabad",
               "state_district": "Hyderabad",
               "state": "Telangana",
               "postcode": 500033,
               "country": "India",
               "country_code": "in"
          },
           "boundingbox": [
              17.421132,
              17.4213237,
              78.4005923,
              78.4007423
          ]
      }
      As you can see "lat" is field in the top level of this JSON object. "house_number" is not. You first have to fetch "address", and then with its reference you cab fetch "house_number". So something like, obj.item("address").item("house_number").ToString
      Preferred -> Jon's Plugins, Pushover, Phlocation, Easy-trigger,
      Rfxcom, Blade Plugins, Pushbullet, homekit, Malosa Scripts




      HS3Pro 4.1.14.0 on windows 10 enterprise X64 on hp quadcore laptop 8 GB.

      Comment


        #4
        How does it work with this ?
        I did fetch rows first,but when i want to fetch distance, i get an error.

        i also used :
        JSON = JSON.replace("[","")
        JSON = JSON.replace("]","")
        to remove the ]


        Code:
        {
           "destination_addresses" : [ "test, 34343434 nz test, country" ],
           "origin_addresses" : [ "test 14-16, 333333 nz test,country" ],
           "rows" : [
              {
                 "elements" : [
                    {
                       "distance" : {
                          "text" : "1 m",
                          "value" : 0
                       },
                       "duration" : {
                          "text" : "1 min.",
                          "value" : 0
                       },
                       "status" : "OK"
                    }
                 ]
              }
           ],
           "status" : "OK"
        }

        Originally posted by crab987 View Post
        Yes, you did forget something.
        This is how JSON looks when you format it for human reading
        [CODE]
        Preferred -> Jon's Plugins, Pushover, Phlocation, Easy-trigger,
        Rfxcom, Blade Plugins, Pushbullet, homekit, Malosa Scripts




        HS3Pro 4.1.14.0 on windows 10 enterprise X64 on hp quadcore laptop 8 GB.

        Comment


          #5
          [] in JSON denote arrays. To access element of an array you either have to loop through them (for loop) or provide array element index (starts from 0). Since I'm not familiar with vb.net syntax, this is the best I can do.

          For example, to access text field in second duration element (1 min.) you would use something like
          Code:
          obj.item("rows")[0].("elements")[1].("duration").("text").ToString

          Comment


            #6
            I have this Google distance matrix api perfect working in php with 3 different original and 3 destination addresses in one call, but I'm very interested in the vb.NET version

            Comment


              #7
              Thanks for the reply ,
              I did try it but it didnt work
              I solved it now by retrieving everything to .item and it works


              Originally posted by crab987 View Post
              [] in JSON denote arrays. To access element of an array you either have to loop through them (for loop) or provide array element index (starts from 0). Since I'm not familiar with vb.net syntax, this is the best I can do.

              For example, to access text field in second duration element (1 min.) you would use something like
              Code:
              obj.item("rows")[0].("elements")[1].("duration").("text").ToString
              Preferred -> Jon's Plugins, Pushover, Phlocation, Easy-trigger,
              Rfxcom, Blade Plugins, Pushbullet, homekit, Malosa Scripts




              HS3Pro 4.1.14.0 on windows 10 enterprise X64 on hp quadcore laptop 8 GB.

              Comment


                #8
                Solved//
                Last edited by Malosa; October 5, 2017, 08:08 AM.
                Preferred -> Jon's Plugins, Pushover, Phlocation, Easy-trigger,
                Rfxcom, Blade Plugins, Pushbullet, homekit, Malosa Scripts




                HS3Pro 4.1.14.0 on windows 10 enterprise X64 on hp quadcore laptop 8 GB.

                Comment

                Working...
                X