Embarcadero RAD Studio XE

解析巢狀JSON文件

JSON文件依資料特性,可以組織成巢狀的、階層式的資料呈現,例如底下資料是Google地圖所呈現的資料:

http://maps.google.com/maps/api/geocode/json?latlng=25.0596967%2C121.5443056&sensor=true&language=zh-TW

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "331",
               "short_name" : "331",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "復興北路",
               "short_name" : "復興北路",
               "types" : [ "route" ]
            },
            {
               "long_name" : "民有里",
               "short_name" : "民有里",
               "types" : [ "administrative_area_level_4", "political" ]
            },
            {
               "long_name" : "松山區",
               "short_name" : "松山區",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "台北市",
               "short_name" : "台北市",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "台灣",
               "short_name" : "TW",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "105",
               "short_name" : "105",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "105台灣台北市松山區復興北路331號",
         "geometry" : {
            "location" : {
               "lat" : 25.0596758,
               "lng" : 121.5444275
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 25.0610247802915,
                  "lng" : 121.5457764802915
               },
               "southwest" : {
                  "lat" : 25.0583268197085,
                  "lng" : 121.5430785197085
               }
            }
         },
         "place_id" : "ChIJLUebXOSrQjQRuotJJCxEw_4",
         "types" : [ "street_address" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "臺北",
               "short_name" : "臺北",
               "types" : [ "colloquial_area", "locality", "political" ]
            },
            {
               "long_name" : "台灣",
               "short_name" : "TW",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "台灣臺北",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 25.2443731,
                  "lng" : 121.7300824
               },
               "southwest" : {
                  "lat" : 24.7900797,
                  "lng" : 121.2826735
               }
            },
            "location" : {
               "lat" : 25.0329694,
               "lng" : 121.5654177
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 25.2443731,
                  "lng" : 121.7300824
               },
               "southwest" : {
                  "lat" : 24.7900797,
                  "lng" : 121.2826735
               }
            }
         },
         "place_id" : "ChIJmQrivHKsQjQR4MIK3c41aj8",
         "types" : [ "colloquial_area", "locality", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "民有里",
               "short_name" : "民有里",
               "types" : [ "administrative_area_level_4", "political" ]
            },
            {
               "long_name" : "松山區",
               "short_name" : "松山區",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "台北市",
               "short_name" : "台北市",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "台灣",
               "short_name" : "TW",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "台灣台北市松山區民有里",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 25.0622186,
                  "lng" : 121.5507804
               },
               "southwest" : {
                  "lat" : 25.0577548,
                  "lng" : 121.5441643
               }
            },
            "location" : {
               "lat" : 25.0601237,
               "lng" : 121.5477044
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 25.0622186,
                  "lng" : 121.5507804
               },
               "southwest" : {
                  "lat" : 25.0577548,
                  "lng" : 121.5441643
               }
            }
         },
         "place_id" : "ChIJY-RkceWrQjQRf_e1uF5LwyA",
         "types" : [ "administrative_area_level_4", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "松山區",
               "short_name" : "松山區",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "台北市",
               "short_name" : "台北市",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "台灣",
               "short_name" : "TW",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "105",
               "short_name" : "105",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "105台灣台北市松山區",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 25.0759635,
                  "lng" : 121.5800758
               },
               "southwest" : {
                  "lat" : 25.0443819,
                  "lng" : 121.5385388
               }
            },
            "location" : {
               "lat" : 25.0552958,
               "lng" : 121.5541264
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 25.0759635,
                  "lng" : 121.5800758
               },
               "southwest" : {
                  "lat" : 25.0443819,
                  "lng" : 121.5385388
               }
            }
         },
         "place_id" : "ChIJkRGtsfKrQjQRYWPBFx-XsMA",
         "types" : [ "administrative_area_level_3", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "105",
               "short_name" : "105",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "松山區",
               "short_name" : "松山區",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "台北市",
               "short_name" : "台北市",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "台灣",
               "short_name" : "TW",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "105台灣台北市松山區",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 25.0759635,
                  "lng" : 121.5800758
               },
               "southwest" : {
                  "lat" : 25.0443819,
                  "lng" : 121.5385388
               }
            },
            "location" : {
               "lat" : 25.0552958,
               "lng" : 121.5541264
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 25.0759635,
                  "lng" : 121.5800758
               },
               "southwest" : {
                  "lat" : 25.0443819,
                  "lng" : 121.5385388
               }
            }
         },
         "place_id" : "ChIJ7WbTsfKrQjQRvw4VhBEwgnk",
         "types" : [ "postal_code" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "台北市",
               "short_name" : "台北市",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "台灣",
               "short_name" : "TW",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "台灣台北市",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 25.2103038,
                  "lng" : 121.6659421
               },
               "southwest" : {
                  "lat" : 24.9605084,
                  "lng" : 121.4570603
               }
            },
            "location" : {
               "lat" : 25.0329636,
               "lng" : 121.5654268
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 25.2103038,
                  "lng" : 121.6659421
               },
               "southwest" : {
                  "lat" : 24.9605084,
                  "lng" : 121.4570603
               }
            }
         },
         "place_id" : "ChIJi73bYWusQjQRgqQGXK260bw",
         "types" : [ "administrative_area_level_1", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "台灣",
               "short_name" : "TW",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "台灣",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 26.3836884,
                  "lng" : 123.4934282
               },
               "southwest" : {
                  "lat" : 20.5862202,
                  "lng" : 116.7118602
               }
            },
            "location" : {
               "lat" : 23.69781,
               "lng" : 120.960515
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 25.2996232,
                  "lng" : 122.0071612
               },
               "southwest" : {
                  "lat" : 21.8066129,
                  "lng" : 120.0350435
               }
            }
         },
         "place_id" : "ChIJL1cHXAbzbjQRaVScvwTwEec",
         "types" : [ "country", "political" ]
      }
   ],
   "status" : "OK"
}

Delphi程式碼

unit Unit2;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, JSON, Vcl.StdCtrls, idHttp;

type
  TForm2 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
var
  s: String;
  HTTP: TIdHTTP;

  jso:TJSONObject;
  jv:TJSONValue;
  jstr:string;//要轉換的json字串

begin

  HTTP := TIdHTTP.Create;
  //s:='http://192.192.246.169/';

  s := 'http://maps.google.com/maps/api/geocode/json?latlng=25.0596967%2C121.5443056&sensor=true&language=zh-TW';
  memo1.Lines.Text := HTTP.Get(s);

  jso:=nil;
  //jstr:='{"name":"陳富國","Interest":"閱讀"}';

  jstr :=   memo1.Lines.Text;
   try
      jso:=TJSONObject.Create;
      jso:=TJSONObject.ParseJSONValue(tencoding.utf8.getbytes(jstr),0) as tjsonobject;
      jv:=jso.get('status').JsonValue;
      ShowMessage('Status = ' + jv.Value);
   finally
    jso.Free;
   end;
end;

end.