JSON with RadStudio Delphi or C++ Builder

15 Responses

  1. Fabio Watanabe says:

    Hello, how are you?
    Congratulations on your article.
    To create the JSON object was perfect!
    Putting JSON Object in Memo was perfect: Memo1.Lines.Text: = Books.ToString;

    The big problem is reading the string created in Memo1.

    I am using Delphi 2010 and to get the get method (a: = TJSONArray (o.Get (‘books’). JsonValue)); it does not pass ‘books’ as it is expecting an integer;

    It also doesn’t pass here (for idy: = 0 to begin (book.Count) pred), as book has no count method;

    Here it also complains (ShowMessage (book.Pairs [idy] .JsonString.ToString + ‘:’ + book.Pairs [idy] .JsonValue.ToString);),

    because it says there is no PAIRS.

    Based on the JSON that I was able to create with your tip, would you show me how to read it to get the “Municipality Name” in “ListMunicipio”?

       “CityList”: [
                        “ID”: “4100103”,
                        “UF”: “PR”,
                        “MunicipalityName”: “ABATIA”
                        “ID”: “4100202”,
                        “UF”: “PR”,
                        “MunicipalityName”: “ADRIANOPOLIS”

    I am using Delphi 2010 with unit DBXJSON, DBXJSONReflect;
    Thank you!

    • I’m afraid I don’t have a copy of D2010 to try, and I am not surprised there are incompatibilities with the much newer json libs. I will share your questions to see if someone can help.

      • Fabio Watanabe says:

        Thank you so much for your attention. I will continue researching and if you find the solution, in this version that I am using (2010), I will post here to help the community.

  2. steveandamyalso says:

    I guess this code works for an incredibly simple JSON chunk of data, but it completely falls apart if the data is the least bit more complicated. I wish there were some decent examples out there.

    • I’ve used this code for very in-depth json parsing.
      Perhaps include the json you’re having difficulty with in the comments?

      • steveandamyalso says:

        I *think* the JSON data is ‘corrupt’ or incorrectly built. I noticed your array of JSON objects (…did I word it correctly?) is surrounded by “[]”, which you mentioned in your writ, and referenced by “books”. The (singular) JSON data I have starts like the following:

        The parser did not like the JSON data. As soon as I removed the “[]”, the parser worked as advertised (imagine my relief).

        • The square brackets [] indicate an array of objects.

          In the source code samples, I am demonstrating parsing a single object with a ‘books’ property, where books is an array.

          You could alter the code to read an array directly, instead of an object containing an array.

          This is untested but something like this would do the trick:

          a := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(Memo1.Lines.Text),0) as TJSONArray;

  3. stuart@tolydigital.net says:

    Thank you.

  4. stuart@tolydigital.net says:

    I found ms-help://embarcadero.rs_xe4/libraries/Data.DBXJSON.html#Classes
    and the for loop becomes:

    for (int idy = 0; idy Size(); idy++) {
    ShowMessage( book->Get(idy)->JsonString->ToString() + ‘:’ +
    book->Get(idy)->JsonValue->ToString() );

  5. stuart@tolydigital.net says:

    The posting corrupted the include file names – System.JSON.hpp and Data.DBXJSON.hpp

  6. stuart@tolydigital.net says:

    I’m using C++ Builder XE4 which lacks “include “. I found “include ” which is a partial implementation of JSON but does not the Book Pairs method or the Book Count property. Suggestions please?

Leave a Reply