当前位置: 首页 > 面试题库 >

如何在Delphi XE2中解析嵌套的JSON对象?

申屠项明
2023-03-14
问题内容

我是JSON的新手,我手上有这个项目,需要我解析JSON并将其某些内容显示在ListView中。问题是我现在阅读的文档处理的是包含JSON数组的JSON对象,而我的案例涉及处理嵌套对象。简而言之,这里是摘要:我将Delphi
XE2与DBXJSON一起使用。我将一些值发布到服务器,它使用如下所示的JSON对象进行回复:

    {
    "products": {
        "Men's Sneakers": {
            "instock": false,
            "size": "423",
            "manufacturer": "Adidas",
            "lastcheck": "20120529"
        },
        "Purse": {
            "instock": true,
            "size": "not applicable",
            "manufacturer": "Prada",
            "lastcheck": "20120528"
        },
        "Men's Hood": {
            "instock": false,
            "size": "M",
            "manufacturer": "Generic",
            "lastcheck": "20120529"
       }
    },
   "total": 41,
   "available": 30
}

我想要实现的是解析每个项目(即“钱包”)并将其作为标题添加到一个列表视图中,以及一个子项目(制造商)。我创建了一个以JSON字符串为参数的过程,创建了JSON对象,但是我不知道如何进一步解析嵌套的对象。

procedure TForm1.ParseString(const AString: string);
var
  json          : TJSONObject;
  jPair         : TJSONPair;
  jValue        : TJSONValue;
  jcValue       : TJSONValue;
  l,i           : Integer;
begin
    json    := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(AString),0) as TJSONObject;
  try
    //get the pair to evaluate in this case the index is 1
    jPair   := json.Get(1); 
        {further process the nested objects and adding them to the listview}
  finally
     json.Free;
  end;
end;

任何建议将不胜感激。浪费了很多时间来尝试在Delphi中获取JSON的详细信息,但无济于事。

谢谢,sphynx


问题答案:

试试这个样本

{$APPTYPE CONSOLE}

{$R *.res}

uses
  DBXJSON,
  System.SysUtils;


Const
StrJson=
'{'+
'    "products": {'+
'        "Men''s Sneakers": {'+
'            "instock": false,'+
'            "size": "423",'+
'            "manufacturer": "Adidas",'+
'            "lastcheck": "20120529"'+
'        },'+
'        "Purse": {'+
'            "instock": true,'+
'            "size": "not applicable",'+
'            "manufacturer": "Prada",'+
'            "lastcheck": "20120528"'+
'        },'+
'        "Men''s Hood": {'+
'            "instock": false,'+
'            "size": "M",'+
'            "manufacturer": "Generic",'+
'            "lastcheck": "20120529"'+
'        }'+
'    },'+
'    "total": 41,'+
'    "available": 30'+
'}';

procedure ParseJson;
var
  LJsonObj  : TJSONObject;
  LJPair    : TJSONPair;
  LProducts : TJSONValue;
  LProduct  : TJSONValue;
  LItem     : TJSONValue;
  LIndex    : Integer;
  LSize     : Integer;
begin
    LJsonObj    := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(StrJson),0) as TJSONObject;
  try
     LProducts:=LJsonObj.Get('products').JsonValue;
     LSize:=TJSONArray(LProducts).Size;
     for LIndex:=0 to LSize-1 do
     begin
      LProduct := TJSONArray(LProducts).Get(LIndex);
      LJPair   := TJSONPair(LProduct);
      Writeln(Format('Product Name %s',[LJPair.JsonString.Value]));
        for LItem in TJSONArray(LJPair.JsonValue) do
        begin
           if TJSONPair(LItem).JsonValue is TJSONFalse then
            Writeln(Format('  %s : %s',[TJSONPair(LItem).JsonString.Value, 'false']))
           else
           if TJSONPair(LItem).JsonValue is TJSONTrue then
            Writeln(Format('  %s : %s',[TJSONPair(LItem).JsonString.Value, 'true']))
           else
            Writeln(Format('  %s : %s',[TJSONPair(LItem).JsonString.Value, TJSONPair(LItem).JsonValue.Value]));
        end;
     end;
  finally
     LJsonObj.Free;
  end;
end;

begin
  try
    ParseJson;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  Readln;
end.

这将返回

Product Name Men's Sneakers
  instock : false
  size : 423
  manufacturer : Adidas
  lastcheck : 20120529
Product Name Purse
  instock : true
  size : not applicable
  manufacturer : Prada
  lastcheck : 20120528
Product Name Men's Hood
  instock : false
  size : M
  manufacturer : Generic
  lastcheck : 20120529


 类似资料:
  • 问题内容: 我正在尝试解析JSON对象,其中的一部分看起来像这样: 等等.... 到目前为止,通过执行以下操作,我可以非常简单地解析: 但是,当我尝试将其用作“名称:”时,它将不起作用。 我试过了: 当我尝试这样做时,我得到“找不到JSONObject [业务]”。 当我尝试: 我得到了预期的“找不到JSONObject [名称]”。 我在这里做错了什么?我缺少一些基本的东西。 问题答案: 好吧,

  • 问题内容: 我们的团队决定使用Retrofit 2.0, 并且我正在对该库进行一些初步研究。如标题中所述,我想通过Android应用中的Retrofit 2.0解析一些嵌套的JSON对象。 例如,这是一个嵌套的JSON对象,其格式为: 我对数组内的JSON对象感兴趣。我注意到有一些关于通过Retrofit 1.X解析嵌套JSON对象的文章,但是最新的Retrofit 2.0API发生了很大变化,这

  • 编写我自己的JSON转换器工厂,它扩展converter.factory. 以字符串类型返回原始响应,并自己解析。但是根据我最初的研究,从改型2.0中得到原始的响应并不容易。Retrofit 2.0似乎坚持在将响应传递给我之前将其转换为某个内容,而且Retrofit没有提供自己的。(我可能错了~) Update:我们实际上可以通过将设置为HTTP API接口的pojo,并使用Retrofit提供的

  • 问题内容: 我有一个与此对象相似的JSON对象: 现在,我想将“ name”和“ mother”解析为该结构: 我想用struct标签指定JSON字段名称,但是我不知道该用什么作为标签,因为它不是我感兴趣的顶级对象。我在软件包文档中也没有找到关于此的任何内容博客文章JSON和Go。我还测试,和。 问题答案: 遗憾的是,与包不同,该包没有提供访问嵌套值的方法。您需要创建一个单独的Parents结构或

  • 问题内容: 我正在尝试解析这种结构:(它使我发疯,并且我尝试了我能想到的一切。但是我不是很有经验) “ topDrop”就像文件名吗?player是一个JSONArray,包含5个播放器JSONObject。但是在JSON术语中,最重要的是什么。我在JSON验证程序上签出有效的凭证,我需要这样的凭证: topDrop作为JSONObject Player,作为JSONArray,并循环遍历数组中的

  • 问题内容: 我必须将下面嵌套的Json数组的数据解析到我的应用程序中。我很困惑如何从中获取价值。 任何人都可以指导我如何从中获取内部价值。 我已经试过了 问题答案: 这是我认为您的JSON解析器应为的样子(可能存在一些拼写错误,我没有在编辑器上测试此代码:)):