JSON基础知识

{"name":"张三", "other":["中国","程序员"]}

 以一对花挎号{},{} 之间是一个对象, 对象可以层层嵌套

元素与值用冒号 ":" 隔开
元素与元素用逗号 "," 隔开


要根据JSON文本结构,才来Delphi操作JSON三步曲:  

//1引用单元
uses System.JSON;
//2解析 文本
var aJSONObject: TJSONObject;
begin
   aJSONObject := TJSONObject.ParseJSONValue(TEncoding.UTF8.GetBytes(
   '{"name":"张三", "other":["中国","程序员"]}'
   ), 0) as TJSONObject;
//3.1获得 JSON 对象 值
showmessage(  aJSONObject.GetValue('name').Value); 

   3.2获得一维数组的值 
var   m_JSON_Array: TJSONArray;
begin  
 m_JSON_Array := aJSONObject.GetValue('other') as TJSONArray;
showmessage( m_JSON_Array.Items[0].Value);  // 中国
   3.3获得一维数组的所有值 

  var i:integer;
  for I := 0 to m_JSON_Array.Count -1 do
  m_Str:=m_Str+'_'+ m_JSON_Array.Items[i].Value   //"中国","程序员"  

image


 

 image 

json 中数组[]:
[] 表示数组, 数组元素用逗号 "," 隔开,值可以是 string、number、object、array、true、false、null; 其中 string 要使用双引号(也有语言可以用单引号)

{"weather_data": [
    {        "riddle": "做砖的坯子、插刀的鞘子",
        "answer": "框框套套"
    },
    {        "riddle": "做贼碰上劫路人",
        "answer": "坏到一块了;冤家路窄"
    },
    {        "riddle": "做贼的说梦话",
        "answer": "想偷"
    },
    {        "riddle": "做贼的不用化装",
        "answer": "贼眉贼眼"
    },
    {        "riddle": "做贼盗黄连",
        "answer": "自讨苦吃"
    }
]
}

    uses System.JSON  , System.Generics.Collections 

var
  m_JSONObject: TJSONObject;
  m_Item1, m_Item2 : TJSONValue;
  m_JSON_Array: TJSONArray;
  m_Str: string;
  I: Integer;
begin
  // 读JSON文本
  Memo1.Lines.LoadFromFile('xiehouyu.json', TEncoding.UTF8);
  m_Str := Trim(Memo1.text);

  m_JSONObject := TJSONObject.ParseJSONValue(TEncoding.UTF8.GetBytes(m_Str), 0) as TJSONObject;
  m_JSON_Array := m_JSONObject.GetValue('weather_data') as TJSONArray;

  // 循环读数组数据
  for I := 0 to m_JSON_Array.count - 1 do     begin
    m_Item1 := (m_JSON_Array.Items[I] as TJSONObject).GetValue('riddle');
    m_Item2 := (m_JSON_Array.Items[I] as TJSONObject).GetValue('answer');
    // 输出
   Memo2.Lines.Add(m_Item1.Value+'_'+m_Item2.Value)   ;
  end;
  m_JSONObject.Free;
end; 

 遇到的问题

值里有逗号的问题

image

 先将这两个替换成其他文本

文本里\n,也类似

image

 如果JSON文本有错误

  m_JSON_Array := m_JSONObject.GetValue('Zi_data') as TJSONArray;

这里会报错

image

 FDquery1 导出为JSON格式

var
  i: integer;
  JSONArray: TJSONArray;
  JSONObj: TJSONObject;
  Field: TField;
  StrList: TStringList;
begin
    JSONArray := TJSONArray.Create;
      FDquery1.First;
      while not FDquery1.Eof do
      begin
        JSONObj := TJSONObject.Create;
        for I := 0 to FDquery1.FieldCount - 1 do
        begin
          Field := FDquery1.Fields[I];
          // 根据字段类型转换
          case Field.DataType of
            ftInteger, ftSmallint, ftAutoInc:
              JSONObj.AddPair(Field.FieldName, TJSONNumber.Create(Field.AsInteger));
            ftLargeint:
              JSONObj.AddPair(Field.FieldName, TJSONNumber.Create(Field.AsLargeInt));
            ftFloat, ftCurrency:
              JSONObj.AddPair(Field.FieldName, TJSONNumber.Create(Field.AsFloat));
            ftBoolean:
              JSONObj.AddPair(Field.FieldName, TJSONBool.Create(Field.AsBoolean));
            ftBlob, ftMemo, ftGraphic:
              JSONObj.AddPair(Field.FieldName, Field.AsString); // 或用 Base64
          else
            if Field.IsNull then
              JSONObj.AddPair(Field.FieldName, TJSONNull.Create)
            else
              JSONObj.AddPair(Field.FieldName, Field.AsString);
          end;
        end;
        JSONArray.AddElement(JSONObj);
        FDquery1.Next;
      end;

         // 写入文件
      StrList := TStringList.Create;
        StrList.Text := JSONArray.Format(2); // 2 = 缩进
        StrList.Text:='{'+StrList.Text+'}';//前后  大括号
        StrList.SaveToFile('eee.json', TEncoding.UTF8);
        StrList.Free;
      JSONArray.Free;
  showmessage('导出为eee.json');
end;