简体   繁体   中英

Delphi / SuperObject - Accessing subnodes away returning NIL

I have the following JSON from server:

{
  "SuccessResponse": {
    "Head": {
      "RequestId": "",
      "RequestAction": "GetMultipleOrderItems",
      "ResponseType": "Orders",
      "Timestamp": "2016-05-10T15:13:06-0300"
    },
    "Body": {
      "Orders": {
        "Order": [
          {
            "OrderId": "457634",
            "OrderNumber": "256176682",
            "OrderItems": {
              "OrderItem": {
                "OrderItemId": "712893",
                "ShopId": "14690930",
                "OrderId": "457634",
...

I'm using the following code to access this values:

procedure TForm1.GetOrdersPendingItems;
var
  mydata : string;
  obj, orderObj: ISuperObject;
  orderArray: TSuperArray;
begin
  mydata := GetURLAsString(GenerateApiUrl('GetMultipleOrderItems', 'OrderIdList', '[457634,457817]'));
  obj := SO(mydata);

  orderObj := obj['SuccessResponse.Body.Orders.Order'];
end;

With this code, if I use a simple Label1.Caption := orderObj.AsString; , it show me this:

"OrderId": "457634",
"OrderNumber": "256176682",
"OrderItems": {
   "OrderItem": {
       "OrderItemId": "712893",
       "ShopId": "14690930",
       "OrderId": "457634",
...

By the logic, the values inner of OrderItem can be access like this: orderObj['OrderItems.OrderItem']; , but if I try to access a "easy" value like OrderId , that is the first element, using orderObj['OrderId']; it returns nil and the same happens with all nodes of the orderObj ...

So, the values in the orderObj.AsString can't be accessed to convert into variable...

There are a way to access the value inner of OrderItem ? My objective is convert the values of OrderItem into a ClientDataSet using the following code:

orderArray := orderObj.AsArray;
TJSONDB.JsonToClientDataSet(orderArray, cdsOrdersItems);

Thanks!

Here you mention this:

By the logic, the values inner of OrderItem can be access like this: orderObj['OrderItems.OrderItem'];

This would work, indeed.
But right after you wrote this contradicting the last sentence:

but if I try to access a "easy" value like OrderId, that is the first element, using orderObj['OrderId'];

By the logic, as you say, to access the values you could do:

orderObj['OrderItems.OrderItem.OrderId'];

and not orderObj['OrderId']; directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM