Delphi:JSON 数组 | 珊瑚贝

Delphi: JSON array


试图理解 Delphi 中的 JSON。使用模块”DBXJSON.pas”。如何使用它来制作这样的数组:

1
2
3
4
Array:[
        {“1″:1_1,”1_2_1”:1_2_2},
        …,
   ]

这样做:

1
2
3
4
5
6
JSONObject:=TJSONObject.Create;
JSONArray:=TJSONArray.Create();

JSONArray.AddElement(TJSONObject.Create(TJSONPair.Create(‘1’,‘1_1’)));
JSONArray.AddElement(TJSONObject.Create(TJSONPair.Create(‘1_2_1’,‘1_2_2’)));
JSONObject.AddPair(‘Array’,JSONArray);

,但得到这个:

1
2
3
4
5
{
“Array”:[
{“1″:”1_1”}
,{“1_2_1″:”1_2_2”}
]
}

请帮忙!
谢谢!

  • 使用 Delphi 附带的任何东西都有明显的优势,但是如果你要做很多 JSON 的东西,你真的应该看看 SuperObject。它在很多方面都很棒。 code.google.com/p/superobject/source/checkout
  • 您声称希望在 1_1 的对象中有一个值。这不是一个有效的 Javascript 值。数字文字不允许包含下划线。 (它们在 Java 和 Perl 中被允许,但不是 Javascript。)请澄清你真正想要的,因为当你的问题出现时,你想要的根本不是 JSON,所以你不应该使用 JSON 库。


您在上面发布的代码不正确。您已经创建了一个 JSON 数组并尝试将对元素添加到该数组中。但是,您必须将 JSON Objects 添加到此数组中,而不是将对添加到数组中,并且这些对象必须包含您的对。
这是解决您的问题的示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
program Project3;

{$APPTYPE CONSOLE}

uses
  SysUtils, dbxjson;

var jsobj, jso : TJsonObject;
    jsa : TJsonArray;
    jsp : TJsonPair;
begin
  try
    //create top-level object
    jsObj := TJsonObject.Create();
    //create an json-array
    jsa := TJsonArray.Create();
    //add array to object
    jsp := TJSONPair.Create(‘Array’, jsa);
    jsObj.AddPair(jsp);

    //add items to the _first_ elemet of array
    jso := TJsonObject.Create();
    //add object pairs
    jso.AddPair(TJsonPair.Create(‘1’, ‘1_1’));
    jso.AddPair(TJsonPair.Create(‘1_2_1’, ‘1_2_2’));
    //put it into array
    jsa.AddElement(jso);

    //second element
    jso := TJsonObject.Create();
    //add object pairs
    jso.AddPair(TJsonPair.Create(‘x’, ‘x_x’));
    jso.AddPair(TJsonPair.Create(‘x_y_x’, ‘x_y_y’));
    //put it into array
    jsa.AddElement(jso);

    writeln(jsObj.ToString);
    readln;

  except
    on E: Exception do
      Writeln(E.ClassName, ‘: ‘, E.Message);
  end;
end.

输出为

1
2
3
4
5
{“Array”:[
     {“1″:”1_1″,”1_2_1″:”1_2_2”}
,
     {“x”:”x_x”,”x_y_x”:”x_y_y”}
  ]
}

与@teran 相同的答案:

更改:

1
2
JSONArray.AddElement(TJSONObject.Create(TJSONPair.Create(‘1’,‘1_1’)));
JSONArray.AddElement(TJSONObject.Create(TJSONPair.Create(‘1_2_1’,‘1_2_2’)));

到:

1
2
JSONArray.AddElement(TJSONPair.Create(‘1’,‘1_1’));
JSONArray.AddElement(TJSONPair.Create(‘1_2_1’,‘1_2_2’));

干杯。

  • AddElement 方法需要一个 TJSONValue,但 TJSONPair 不是 TJSONValue 的子类,因此您建议的代码甚至不会编译。所需的输出是数组包含一个值。您正在尝试将对直接添加到数组中,但对不是值。那么,您的答案与 Teran 的答案有何相同之处?
  • @Rob Kennedy 好的,有一个错误,想法是删除额外的级别对象


来源:https://www.codenong.com/10549935/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9530.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?