在Delphi中解析JSON数组 [英] Parsing JSON Array in Delphi

查看:905
本文介绍了在Delphi中解析JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与 Delphi解析JSON数组或数组有类似的问题,但是答案不符合我的需求.

I have a similar problem to Delphi parse JSON array or array, but the answers do not fit my need.

我有这个JSON:

[
  {
    "total": "9",
    "page": "9",
    "records": "99",
    "rows": [
      {
        "id": "62316",
        "titleId": "47243",
        "subject": [
          "000607",
          "000607_",
          "001727"
        ],
        "keyFeatures": [
          "AI",
          "URL"
        ]
      },
      {
        "id": "66",
        "titleId": "47243",
        "subject": [
          "000607",
          "000607_",
          "001727"
        ],
        "keyFeatures": [
          "KK"
        ]
      }
    ],
    "suggestion": "90"
  }
]

我想写全部备忘录中每个"id"的"keyFeatures",如下所示:

I would like to write all f.e. "keyFeatures" of every "id" in a memo, like this:


1: 62316   KeyFeatures: AI,URL
2: 66      KeyFeatures: KK   

procedure TIFForm1.ParseJson(StrJson: string);
var
  LJsonArr: TJSONArray;
  LJsonArrRow: TJSONArray;
  vJSONObject: TJSONObject;
  vJSONPair: TJSONPair;
  vJSONObjectKF: TJSONObject;
  vJSONPairKF: TJSONPair;
  vJasonArrRows: TJSONArray;
  vJSONValue: TJSONValue;
  LJSONAttribute: String;
  LJSONKF_key: String;
  LJSONValue: String;
  i: integer;
begin
  LJsonArr := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(StrJson), 0) as TJSONArray;
  if LJsonArr <> nil then
  try
    // rows
    vJasonArrRows := LJsonArr as TJSONArray;
    for vJSONValue in vJasonArrRows do
    begin
      i := i + 1;
      vJSONObject := vJSONValue as TJSONObject;
      vJSONPair := vJSONObject.get('id');
      LJSONAttribute := vJSONPair.JsonString.Value;
      vJSONValue := vJSONPair.JsonValue.Value;
      vJSONObjectKF := vJSONValue as TJSONObject;
      vJSONPairKF := vJSONObject.Get('keyFeatures');
      LJSONKF_key := vJSONPairKF.JsonString.Value;
      // How can I here merge all keyFeatures together with , separated?
      //Edit: My Serializer
      rowresult.KeyFeatures := serialize(vJSONObject, 'keyFeatures');
      Memo5.Lines.Add(Format('%d: %s KeyFeatures: %s', [i, rowresult.id, rowresult.keyFeatures]));
    end;
  finally
    LJsonArr.Free;
  end;
end;

此外,如果我可以要求JSON元素的类型,那将很方便. 这是一个带有keyFeatures的示例,它又是一个JSON数组.但是可能还有更多未知的命名JSON键,它们也都是数组,这些键也应该写在备忘录中.有解决办法吗?

Moreover, it would be handy if I could ask for the type of the JSON elements. Here is an example with keyFeatures which is again an JSON Array. But there could be more unknown named JSON keys which are also array, and these ones should also be written in the Memo. Is there a solution?

我已经在DP解答的帮助下以这种方式解决了该问题,请看下面.

I have solved it this way with the help from the DP Answer, look below.

function TIFForm1.serialize(MyJSONObject: TJSONObject; keystring: string): string;
var
  KeyFeatures: TJSONValue;
  FeatureList: TStringList;
  FeatureItem: TJSONValue;
begin
  KeyFeatures := (MyJSONObject as TJSONObject).GetValue(keystring);
  if KeyFeatures is TJSONArray then
  begin
    FeatureList := TStringList.Create;
    try
      for FeatureItem in TJSONArray(KeyFeatures) do
        FeatureList.Add(FeatureItem.Value);
      Result := FeatureList.CommaText;
    finally
      FeatureList.Free;
    end;
  end
  else
  begin
    Result := KeyFeatures.Value;
  end;
end;

推荐答案

来自Delphipraxis的好人

A nice guy from Delphipraxis DP has provided a very sophisticated solution:

     procedure TForm1.Button1Click(Sender: TObject);
     var
       DataBase: String;
       JsonArray: TJSONArray;
       ArrayElement: TJSonValue;
       RowValue: TJSonValue;
       RowItem: TJSonValue;
       keyFeatures: TJSonValue;
       FeatureItem: TJSonValue;
       FeatureList: TStringlist;
       Id: Integer;
       Index: Integer;
     begin
       Memo1.Clear;
       DataBase :=
         '[{"total":"9","page":"9","records":"99","rows":[{"id":"62316","titleId":"47243","subject":'
         + '["000607","000607_","001727"],"keyFeatures":["AI","URL"]},{"id":"66","titleId":"47243","subject":'
         + '["000607","000607_","001727"],"keyFeatures":["KK"]}],"suggestion":"90"}]';

       JsonArray := TJSonObject.ParseJSONValue(DataBase) as TJSONArray;
       try
         Index := 1;
         for ArrayElement in JsonArray do
         begin
           RowValue := (ArrayElement as TJSonObject).GetValue('rows');
           if RowValue is TJSONArray
           then
           begin
             for RowItem in TJSONArray(RowValue) do
             begin
               RowItem.TryGetValue('id', Id);
               keyFeatures := (RowItem as TJSonObject).GetValue('keyFeatures');
               if keyFeatures is TJSONArray
               then
               begin
                 FeatureList := TStringlist.Create;
                 try
                   for FeatureItem in TJSONArray(keyFeatures) do
                     FeatureList.Add(FeatureItem.Value);
                   Memo1.Lines.Add(Format('%d: %d KeyFeatures: %s', [Index, Id, FeatureList.CommaText]));
                 finally
                   FeatureList.Free;
                 end;
               end;
             end;
           end;
           inc(Index);
         end;
       finally
         JsonArray.Free;
       end;
     end;

这篇关于在Delphi中解析JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆