1

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!

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Zero
  • 533
  • 3
  • 13
  • Which version of superobject do you use and from which site? I also had a lot of problems with the version I once used (v.1.2) that some fields could not be derived. So I switched to xsuperobject available here: [link](https://github.com/onryldz/x-superobject) – Andre Ruebel May 10 '16 at 20:57
  • @AndreRuebel, I'm using the version 1.2 from this [link](https://github.com/hgourvest/superobject), I will test the xsuperobject and post the result. – Zero May 10 '16 at 21:07
  • Yes, that's the one I used, too. It seems to have parsing problems under special conditions. It is quite a while ago, so I cannot recall the exact conditions under which I had problems. Unfortunately you will have to change your code to use xsuperobject, since the approach is somewhat different. But it works for me ever since. – Andre Ruebel May 10 '16 at 21:17
  • @AndreRuebel, It worked fine, but the `TJSONDB.JsonToClientDataSet(orderArray, cdsOrdersItems);` needs to be rewritten.. hahaha, it will be good, coz' the code for convert json to clientdataset gives problems too, I'll do it with simple methods... But now I don't know what to do with this question, it's my first question here... – Zero May 10 '16 at 21:42
  • Hi Zero, welcome to SO. Not a bad first question. If you problem is solved, please leave a comment saying your problem is solved and the we'll take care of the rest. – Johan May 11 '16 at 00:35
  • Hi @Johan, thank you. My problem is solved. – Zero May 11 '16 at 01:56

1 Answers1

0

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.

henrique romao
  • 560
  • 1
  • 8
  • 32
  • yup, it's right, in this case, I need to do like this: `orderObj['OrderItems.OrderItem[0].OrderId'];`. In this response i got only one item, but in case of more... – Zero Aug 10 '17 at 19:19