delphi json生成和解析

delphi json生成和解析

使用DELPHI 自带的JSON类。

{
    "date": "2014-03-04",
    "error": 0,
    "results": [
        {
            "currentCity": "成都",
            "weather_data": [
                {
                    "date": "周二(今天, 实时:12℃)",
                    "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/duoyun.png",
                    "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/duoyun.png",
                    "temperature": "15 ~ 6℃",
                    "weather": "多云",
                    "wind": "北风微风"
                },
                {
                    "date": "周三",
                    "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/yin.png",
                    "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/xiaoyu.png",
                    "temperature": "14 ~ 7℃",
                    "weather": "阴转小雨",
                    "wind": "北风微风"
                },
                {
                    "date": "周四",
                    "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/xiaoyu.png",
                    "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/xiaoyu.png",
                    "temperature": "12 ~ 7℃",
                    "weather": "小雨",
                    "wind": "北风微风"
                },
                {
                    "date": "周五",
                    "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/xiaoyu.png",
                    "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/xiaoyu.png",
                    "temperature": "9 ~ 6℃",
                    "weather": "小雨",
                    "wind": "南风微风"
                }
            ]
        }
    ],
    "status": "success"
}

  

uses
System.JSON
var
MJsonTxt:string;
MJson:TJSONObject ;
Value,Value2:string;
begin
 MJsonTxt :=Memo1.Lines .Text ;
 MJson :=TJSONObject .ParseJSONValue(MJsonTxt )as TJSONObject ;
 if Assigned(MJson ) then
 begin
   Value :=MJson .GetValue<string>('results[0].weather_data[1].temperature');

   ShowMessage(Value ); //14 ~ 7℃

  FreeAndNil(MJson );
 end;
end;

 

var jr:tjsonarray;

jr := jo.GetValue<TJSONArray>('data.dataList') ;

      for i := 0 to jr.Count - 1 do
  begin
    jrow := jr.Get(i) as TJSONObject;
  self.Caption:=  jrow.GetValue<string>('ampm');

end;

 

 
uses System.JSON;

procedure TForm1.Button1Click(Sender: TObject);
//解析JSON
begin
  var jo: TJSONObject := TJSONObject.ParseJSONValue('{"name":"张三", "other":["中国","程序员"]}') as TJSONObject;  //从字符串生成JSON
  Memo2.Lines.Add('遍历JSON数据:');
  Memo2.Lines.Add('JSON数据数量:' + IntToStr(jo.Count));
  var tmp: string;
  for var i: integer := 0 to jo.Count - 1 do    //1,遍历JSON数据
    tmp := tmp + jo.Get(i).ToString;
  Memo2.Lines.Add(tmp);
  Memo2.Lines.Add('');
  Memo2.Lines.Add('按元素解析JSON数据:');  //2,按元素解析JSON数据
  tmp := 'name = ' + jo.Values['name'].value;
  Memo2.Lines.Add(tmp);
  var ja: TJSONArray := TJSONArray(jo.GetValue('other'));   // json数组
  tmp := 'other = ' + jo.GetValue('other').ToString + #13#10; // 得到JSON数组字符串
  for var i: integer := 0 to ja.Size - 1 do    // 循环取得JSON数组中每个元素
    tmp := tmp + IntToStr(i + 1) + ' : ' + ja.Items[i].Value + #13#10;
  Memo2.Lines.Add(tmp);
  jo.Free;
end;

procedure TForm1.Button2Click(Sender: TObject);
//生成JSON
begin
  var jo: TJSONObject := TJSONObject.Create;
  jo.AddPair('name','张三');
 // var ja: TJSONArray := TJSONObject.ParseJSONValue('["中国","程序员"]') as TJSONArray;
  var ja: TJSONArray := TJSONArray.Create;
  ja.Add('中国');
  ja.Add('程序员');
  jo.AddPair('other', ja);
  Memo2.Lines.Add(jo.ToString); //{"name":"张三","other":["中国","程序员"]}
  Memo2.Lines.Add(jo.ToJSON);   //{"name":"\u5F20\u4E09","other":["\u4E2D\u56FD","\u7A0B\u5E8F\u5458"]}
  jo.Free;
end;

  

 

const
  s = '[{"id":1,"name":"咏南"},{"id":2,"name":"中间件"}]';

procedure newFields(const ds: TDataSet; const ja: TJSONArray; const strFieldSize: Integer = 256);
begin
  var jo: TJSONObject := ja.Items[0] as TJSONObject;
  ds.Close;
  ds.FieldDefs.Clear;
  for var pair: TJSONPair in jo do
  begin
    var fieldName: string := pair.JsonString.Value;
    var fieldSize: Integer := 0;
    if pair.JsonValue is TJSONString then
    begin
      fieldSize := strFieldSize;
      ds.FieldDefs.Add(fieldName, ftString, fieldSize)
    end
    else if pair.JsonValue is TJSONNumber then
    begin
      ds.FieldDefs.Add(fieldName, ftFloat);
    end;
  end;
  if ds is TClientDataSet then
    TClientDataSet(ds).CreateDataSet;
end;

 

procedure TForm2.Button1Click(Sender: TObject);
begin
  var s: string := '{"name":"咏南中间件","age":12}';
  var jo: TJSONObject := TJSONObject.Create;
  jo.AddPair('f1', s);
  Memo1.Lines.add(jo.ToJSON);   //{"f1":"{\"name\":\"\u548F\u5357\u4E2D\u95F4\u4EF6\",\"age\":12}"}

  var jo2: TJSONObject := TJSONObject.ParseJSONValue(jo.ToString) as TJSONObject;
  s := jo2.tostring;               //{"f1":"{\"name\":\"咏南中间件\",\"age\":12}"}
  s := StringReplace(s, '\"', '"', [rfReplaceAll]);
  memo1.Lines.add(s);   //{"f1":"{"name":"咏南中间件","age":12}"}
  jo.free;
  jo2.free;
end;

  

 

 

posted @ 2020-03-31 16:10  delphi中间件  阅读(3893)  评论(0编辑  收藏  举报