Results 1 to 4 of 4

Thread: READ json file

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,939

    READ json file

    How to read alla node in this json string:

    Code:
    {
       "results" : 
       [
          {
             "address_components" : 
             [
                {
                   "long_name" : "7",
                   "short_name" : "7",
                   "types" : 
                   [
                      "street_number"
                   ]
                },
                {
                   "long_name" : "Via Toledo",
                   "short_name" : "Via Toledo",
                   "types" : 
                   [
                      "route"
                   ]
                },
                {
                   "long_name" : "Napoli",
                   "short_name" : "Napoli",
                   "types" : 
                   [
                      "locality",
                      "political"
                   ]
                },
                {
                   "long_name" : "Napoli",
                   "short_name" : "Napoli",
                   "types" : 
                   [
                      "administrative_area_level_3",
                      "political"
                   ]
                },
                {
                   "long_name" : "Città Metropolitana di Napoli",
                   "short_name" : "NA",
                   "types" : 
                   [
                      "administrative_area_level_2",
                      "political"
                   ]
                },
                {
                   "long_name" : "Campania",
                   "short_name" : "Campania",
                   "types" : 
                   [
                      "administrative_area_level_1",
                      "political"
                   ]
                },
                {
                   "long_name" : "Italia",
                   "short_name" : "IT",
                   "types" : 
                   [
                      "country",
                      "political"
                   ]
                },
                {
                   "long_name" : "80134",
                   "short_name" : "80134",
                   "types" : 
                   [
                      "postal_code"
                   ]
                }
             ],
             "formatted_address" : "Via Toledo, 7, 80134 Napoli NA, Italia",
             "geometry" : 
             {
                "location" : 
                {
                   "lat" : 40.84798,
                   "lng" : 14.2496255
                },
                "location_type" : "ROOFTOP",
                "viewport" : 
                {
                   "northeast" : 
                   {
                      "lat" : 40.8493328802915,
                      "lng" : 14.2509291302915
                   },
                   "southwest" : 
                   {
                      "lat" : 40.8466349197085,
                      "lng" : 14.2482311697085
                   }
                }
             },
             "navigation_points" : 
             [
                {
                   "location" : 
                   {
                      "latitude" : 40.8479807,
                      "longitude" : 14.2496175
                   }
                }
             ],
             "place_id" : "ChIJeZ7gn1wIOxMR6fvfZkrDK4E",
             "types" : 
             [
                "street_address",
                "subpremise"
             ]
          }
       ],
       "status" : "OK"
    }
    Attached Files Attached Files
    Last edited by dday9; Today at 11:48 AM. Reason: Added code tags

  2. #2
    Frenzied Member
    Join Date
    May 2014
    Location
    Kallithea Attikis, Greece
    Posts
    1,311

    Re: READ json file

    This is the formated text from the script bellow. (using 0 parameter we get the json without white space/newlines)
    Code:
    {    
        "results" : [        
            {            
                "address_components" : [                
                    {                    
                        "long_name" : "7",
                        "short_name" : "7",
                        "types" : [                        
                            "street_number"
                        ]
                    },
                    {                    
                        "long_name" : "Via Toledo",
                        "short_name" : "Via Toledo",
                        "types" : [                        
                            "route"
                        ]
                    },
                    {                    
                        "long_name" : "Napoli",
                        "short_name" : "Napoli",
                        "types" : [                        
                            "locality",
                            "political"
                        ]
                    },
                    {                    
                        "long_name" : "Napoli",
                        "short_name" : "Napoli",
                        "types" : [                        
                            "administrative_area_level_3",
                            "political"
                        ]
                    },
                    {                    
                        "long_name" : "Città Metropolitana di Napoli",
                        "short_name" : "NA",
                        "types" : [                        
                            "administrative_area_level_2",
                            "political"
                        ]
                    },
                    {                    
                        "long_name" : "Campania",
                        "short_name" : "Campania",
                        "types" : [                        
                            "administrative_area_level_1",
                            "political"
                        ]
                    },
                    {                    
                        "long_name" : "Italia",
                        "short_name" : "IT",
                        "types" : [                        
                            "country",
                            "political"
                        ]
                    },
                    {                    
                        "long_name" : "80134",
                        "short_name" : "80134",
                        "types" : [                        
                            "postal_code"
                        ]
                    }
                ],
                "formatted_address" : "Via Toledo, 7, 80134 Napoli NA, Italia",
                "geometry" : {                
                    "location" : {                    
                        "lat" : 40.84798,
                        "lng" : 14.2496255
                    },
                    "location_type" : "ROOFTOP",
                    "viewport" : {                    
                        "northeast" : {                        
                            "lat" : 40.8493328802915,
                            "lng" : 14.2509291302915
                        },
                        "southwest" : {                        
                            "lat" : 40.8466349197085,
                            "lng" : 14.2482311697085
                        }
                    }
                },
                "navigation_points" : [                
                    {                    
                        "location" : {                        
                            "latitude" : 40.8479807,
                            "longitude" : 14.2496175
                        }
                    }
                ],
                "place_id" : "ChIJeZ7gn1wIOxMR6fvfZkrDK4E",
                "types" : [                
                    "street_address",
                    "subpremise"
                ]
            }
        ],
        "status" : "OK"
    }

    I use my interpreter which read the file, then expand to UTF16LE then declare a JsonObject (from JsonObject.cls), and then I do some magic: We can get a JsonArray from then ItemPath. This Array has 8 items, but item 0 has the length of array (.?|.?), We can use itempath from that object and read anything.
    So you need two classes
    https://github.com/M2000Interpreter/...JsonObject.cls
    and
    https://github.com/M2000Interpreter/.../JsonArray.cls

    And follow the methods/functions from the script.
    The idea is: Use the ItemPath.


    Code:
    A=buffer("json1.txt")
    Locale 1033
    Document a$=chr$(A[0, Len(A)])
    //clipboard a$
    declare Json JsonObject
    Json=Json=>parser(a$)^
    clipboard Json=>Json(4)
    M=Json=>ItemPath("results.0.address_components")^
    Print Val(M=>ItemPath("0.long_name"))' 7  = M=>count-1
    For i=1 to  M=>count-1
    Print i," Long name ";M=>ItemPath(i+".long_name")
    Print "","Short name "; M=>ItemPath(i+".short_name")
    next
    Print Json=>ItemPath("status")'"OK"

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,939

    Re: READ json file

    Quote Originally Posted by georgekar View Post
    This is the formated text from the script bellow. (using 0 parameter we get the json without white space/newlines)
    Code:
    {    
        "results" : [        
            {            
                "address_components" : [                
                    {                    
                        "long_name" : "7",
                        "short_name" : "7",
                        "types" : [                        
                            "street_number"
                        ]
                    },
                    {                    
                        "long_name" : "Via Toledo",
                        "short_name" : "Via Toledo",
                        "types" : [                        
                            "route"
                        ]
                    },
                    {                    
                        "long_name" : "Napoli",
                        "short_name" : "Napoli",
                        "types" : [                        
                            "locality",
                            "political"
                        ]
                    },
                    {                    
                        "long_name" : "Napoli",
                        "short_name" : "Napoli",
                        "types" : [                        
                            "administrative_area_level_3",
                            "political"
                        ]
                    },
                    {                    
                        "long_name" : "Città Metropolitana di Napoli",
                        "short_name" : "NA",
                        "types" : [                        
                            "administrative_area_level_2",
                            "political"
                        ]
                    },
                    {                    
                        "long_name" : "Campania",
                        "short_name" : "Campania",
                        "types" : [                        
                            "administrative_area_level_1",
                            "political"
                        ]
                    },
                    {                    
                        "long_name" : "Italia",
                        "short_name" : "IT",
                        "types" : [                        
                            "country",
                            "political"
                        ]
                    },
                    {                    
                        "long_name" : "80134",
                        "short_name" : "80134",
                        "types" : [                        
                            "postal_code"
                        ]
                    }
                ],
                "formatted_address" : "Via Toledo, 7, 80134 Napoli NA, Italia",
                "geometry" : {                
                    "location" : {                    
                        "lat" : 40.84798,
                        "lng" : 14.2496255
                    },
                    "location_type" : "ROOFTOP",
                    "viewport" : {                    
                        "northeast" : {                        
                            "lat" : 40.8493328802915,
                            "lng" : 14.2509291302915
                        },
                        "southwest" : {                        
                            "lat" : 40.8466349197085,
                            "lng" : 14.2482311697085
                        }
                    }
                },
                "navigation_points" : [                
                    {                    
                        "location" : {                        
                            "latitude" : 40.8479807,
                            "longitude" : 14.2496175
                        }
                    }
                ],
                "place_id" : "ChIJeZ7gn1wIOxMR6fvfZkrDK4E",
                "types" : [                
                    "street_address",
                    "subpremise"
                ]
            }
        ],
        "status" : "OK"
    }

    I use my interpreter which read the file, then expand to UTF16LE then declare a JsonObject (from JsonObject.cls), and then I do some magic: We can get a JsonArray from then ItemPath. This Array has 8 items, but item 0 has the length of array (.?|.?), We can use itempath from that object and read anything.
    So you need two classes
    https://github.com/M2000Interpreter/...JsonObject.cls
    and
    https://github.com/M2000Interpreter/.../JsonArray.cls

    And follow the methods/functions from the script.
    The idea is: Use the ItemPath.


    Code:
    A=buffer("json1.txt")
    Locale 1033
    Document a$=chr$(A[0, Len(A)])
    //clipboard a$
    declare Json JsonObject
    Json=Json=>parser(a$)^
    clipboard Json=>Json(4)
    M=Json=>ItemPath("results.0.address_components")^
    Print Val(M=>ItemPath("0.long_name"))' 7  = M=>count-1
    For i=1 to  M=>count-1
    Print i," Long name ";M=>ItemPath(i+".long_name")
    Print "","Short name "; M=>ItemPath(i+".short_name")
    next
    Print Json=>ItemPath("status")'"OK"

    Not complete undersatand this code:

    Code:
    A=buffer("json1.txt")
    Locale 1033
    Document a$=chr$(A[0, Len(A)])
    //clipboard a$
    declare Json JsonObject
    Json=Json=>parser(a$)^
    clipboard Json=>Json(4)
    M=Json=>ItemPath("results.0.address_components")^
    Print Val(M=>ItemPath("0.long_name"))' 7  = M=>count-1
    For i=1 to  M=>count-1
    Print i," Long name ";M=>ItemPath(i+".long_name")
    Print "","Short name "; M=>ItemPath(i+".short_name")
    next
    Print Json=>ItemPath("status")'"OK"
    Last edited by dday9; Today at 11:49 AM. Reason: Added code tags

  4. #4
    Frenzied Member
    Join Date
    May 2014
    Location
    Kallithea Attikis, Greece
    Posts
    1,311

    Re: READ json file

    You have to look the two objects (there are the links to get them),
    For VB6 supposed you have the json1.txt loaded in a string say A$
    Code:
    Dim json as JsonObject, M as JsonArray, i as long
    Set json=new jsonObject
    Set json=json.parser(A$)
    Set M=json.itempath("results.0.address_components")
    For i=1 to M.count-1
    debug.print i, " Long name " & M.ItemPath(i & ".long_name")
    debug.print "", " Long name " & M.ItemPath(i & ".short_name")
    next i
    debug.print json.ItemPath("status")

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width