1

I am using Delphi 10.1 Berlin Update 2, and am trying to use XSuperObject / XSuperJSON to take a JSON response from a 3rd party provider and parse it into an object structure.

Here is the JSON:

[ 
  {
    "yardNumber": 10,
    "links": [
      {
        "rel": "yardSaleList",
        "href": "<url address>"
      }
    ],
    "yardName": "Yard A",
    "auctionDate": "1/25/17"
  },
  {
    "yardNumber": 10,
    "links": [
      {
        "rel": "yardSaleList",
        "href": "<url>"
      }
    ],
    "yardName": "Yard B",
    "auctionDate": "1/25/17"
  }
] 

My code is something like this:

TLinkItem = class
public
  [alias('rel')]
  rel: String;
  [alias('href')]
  href: string;
end;    

TPartItem = class
public
  [alias('yardNumber')]
  YardNumber: integer;
  [alias('links')]
  Links: TObjectList<TLinkItem>;
  [alias('yardName')]
  YardName: string;
  [alias('auctionDate')]
  AuctionDate: String; 
  destructor destroy; override;
end;

TPartItems = class  /// not used because this is an unnamed JSON array
public
  [alias('ItemData')] 
  ItemData : TObjectList<TPartItem>;     
end;

...

destructor TPartItems.destroy;
begin
  freeandnil(Links);
  inherited;
end;

If it were a named array, I could use the above object to refer to the name of the array:

myData := TPartItems.FromJSON(jsonString); 
showmssage(myData.ItemData.count.toString);

But because this is an unnamed array, I can't do that.

I'm hoping I have just missing some kind of detail here that I could not find. Until now, this has worked pretty well with other data suppliers, but I have never run across an unnamed JSON array like this.

5
  • Well strictly speaking, that is valid JSON code. The problem appears to be that XSuperObject missed this implementation, or it just wasn't figured out. After all, you can't reference anything nameless by its name. You may have to bite the bullet and use SO manually the old fashioned way. Commented Jan 10, 2017 at 0:28
  • Welcome to Stack Overflow. What exactly is stopping you from parsing an unnamed array into a TPartItems object? Can you please be more specific? How is TPartItems.FromJSON() actually invoking XSuperObject/XSuperJSON? You did not show that code. People cannot tell you what's wrong if you don't show what you are really doing. Please provide a Minimal, Complete, and Verifiable example. Commented Jan 10, 2017 at 0:29
  • That is what it looks like to me then. I couldn't find anything that talked about it anywhere nor after looking at it the source on how to treat it without going old fashion @RemyLebeau I gave example code? Doesn't parse because there is no such named pair called ItemData, it is unnamed in the JSON. That is my issue. How would I go about declaring the structure of something which has no name. Which it looks XSuperObject doesn't handle. Not sure what else I can give you. I gave the whole object structure and how it would work, so other than that, i just old fashioned it and it works. Commented Jan 10, 2017 at 1:32
  • 2
    @StevenChesser: I was not aware that FromJSON() is a TObject helper method implemented by XSuperObject itself. I thought it was a method in your own code. In that case, I looked at XSuperObject's source and do not see anything that would allow the JSON you have shown to stream an unnamed array into TPartItems directly. So, why not just wrap the JSON array inside of a JSON object that gives the array a name? myData := TPartItems.FromJSON('{ItemData: ' + jsonString + '}'); Commented Jan 10, 2017 at 1:53
  • @Remy always coming up with clever ideas. Commented Jan 10, 2017 at 16:10

3 Answers 3

3

I looked at XSuperObject's source and do not see anything that would allow the JSON you have shown to stream an unnamed array into TPartItems directly. So, I would suggest simply wrapping the JSON array inside of a JSON object that gives the array a name, eg:

myData := TPartItems.FromJSON('{ItemData: ' + jsonString + '}');
Sign up to request clarification or add additional context in comments.

Comments

0

Basicaly you can do addition to @Remy Lebeau

sResponse := '{"arr":'+sResponse+'}'; 
XSO := SO(sResponse);

for I := 0 to XSO.A['arr'].Length-1 do
begin
   XSO.A['arr'].o[i].S['yardNumber'];
   //....
end;

Comments

0

Since I can´t comment, I´ll complement @AhmetSinav

answer:

res := idHttp.get('api/ajustes/127');
objReg := SO(res);
for i := 0 to objReg.A['registros'].Length-1 do
begin
   ShowMessage(objReg.A['registros'].o[i].S['id']);
   //....
end;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.