zoukankan      html  css  js  c++  java
  • JsonDataObjects

    {
      "name": "张三", 
      "age": 33,
      "sex": true,
      "weight": 123.456,
      "tel": ["86-1111111", "86-2222222"],
      "addresses":{"address":"A省B市", 
    "pc":"100001"},
      "children": [
        {
          "name": "张继",
          "age": "22",
          "sex": true
        },
        {
          "name": "张承",
          "age": "11",
          "sex": false
        }
      ]
    }

    uses JsonDataObjects;
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
        jo: TJsonObject;
        i: Integer;
    begin
        jo := TJsonObject.Parse( Memo1.Text) as TJsonObject;
    
        Memo2.Lines.Add(jo['name']);
        Memo2.Lines.Add(jo['age']);
        Memo2.Lines.Add(jo['sex']);
        Memo2.Lines.Add(jo['weight']);
        Memo2.Lines.Add(jo['tel'].Items[0]);
        Memo2.Lines.Add(jo['tel'].Items[1]);
        Memo2.Lines.Add(jo['addresses'].S['address']);
        Memo2.Lines.Add(jo['addresses'].S['pc']);
        Memo2.Lines.Add('总共数据数:' + inttostr(jo['children'].Count));
        for i := 0 to jo['children'].Count - 1 do
        begin
            Memo2.Lines.Add(jo['children'].Items[i]['name']);
            Memo2.Lines.Add(jo['children'].Items[i]['age']);
            Memo2.Lines.Add(jo['children'].Items[i]['sex']);
        end;
        jo.Free;

    ----------------------------------------------------------------------------------
    
    

    Simple example
    var
      Obj: TJsonObject;
    begin
      Obj := TJsonObject.Parse('{ "foo": "bar", "array": [ 10, 20 ] }') as TJsonObject;
      try
        ShowMessage(Obj['foo']);
        ShowMessage(IntToStr(Obj['array'].Count));
        ShowMessage(IntToStr(Obj['array'].Items[0]));
        ShowMessage(IntToStr(Obj['array'].Items[1]));
      finally
        Obj.Free;
      end;
    end;
    
    

    Filling and serializing JSON objects

    
    
    var
      Obj, ChildObj: TJsonObject;
    begin
      Obj := TJsonObject.Create;
      try
        // easy access
        Obj['foo'] := 'bar';
        // normal (and faster) access
        Obj.S['bar'] := 'foo';
        // automatic array creation, Obj is the owner of 'array'
        Obj.A['array'].Add(10);
        Obj.A['array'].Add(20);
        // automatic object creation, 'array' is the owner of ChildObj
        ChildObj := Obj.A['array'].AddObject;
        ChildObj['value'] := 12.3;
        // automatic array creation, ChildObj is the owner of 'subarray'
        ChildObj.A['subarray'].Add(100);
        ChildObj.A['subarray'].Add(200);
    
        ShowMessage(Obj.ToJSON({Compact:=}False));
      finally
        Obj.Free;
      end;
    
    
    {
    	"foo": "bar",
    	"bar": "foo",
    	"array": [
    		10,
    		20,
    		{
    			"value": 12.3,
    			"subarray": [
    				100,
    				200
    			]
    		}
    	]
    }
    
    

    Copying JSON objects with Assign

    
    
    var
      Obj, ClonedObj: TJsonObject;
    begin
      Obj := TJsonObject.ParseUtf8('{ "foo": [ "bar", {}, null, true, false, { "key": "value" } ] }') as TJsonObject;
      try
        ClonedObj := TJsonObject.Create;
        try
          // Make a copy of Obj
          ClonedObj.Assign(Obj);
          ShowMessage(ClonedObj.ToJSON(False));
        finally
          ClonedObj.Free;
        end;
      finally
        Obj.Free;
      end;
    end;
    
    
    {
    	"foo": [
    		"bar",
    		{},
    		null,
    		true,
    		false,
    		{
    			"key": "value"
    		}
    	]
    }




  • 相关阅读:
    vue3.0+vite+ts项目搭建axios封装(六)
    vue3.0+vite+ts项目搭建分环境打包(四)
    vue3.0+vite+ts项目搭建初始化项目(一)
    MFC数值型关联变量和控件型关联变量
    SVN服务端、客服端安装与配置
    (转)ev4加密视频破解 ev4破解工具 ev4转mp4转换器 【无视授权密码即可转换】
    C#中的记录(record)
    C#自定义转换(implicit 或 explicit)
    C#中的隐式转换
    C#中的显式转换
  • 原文地址:https://www.cnblogs.com/xionda/p/15200473.html
Copyright © 2011-2022 走看看