开发者

JSON is not parsing in c++ builder 2010

开发者 https://www.devze.com 2023-02-15 23:40 出处:网络
I\'m starting to work with JSON in builder by including #include<DBXJSON.hpp> doing simple parsings but no result

I'm starting to work with JSON in builder by including #include<DBXJSON.hpp>

doing simple parsings but no result

here is my code

void __fastcall TForm1::okClick(TObject *Sender)
{
    TJSONObject *obj=new TJSONObject();
    TBytes StringBytes = TEncoding::UTF8->GetBytes(Memo1->Text);
    obj->Parse(StringBytes, 0);
    Memo2->Text = obj->ToString();
    obj->~TJSONObject();
} 

my Memo1->Text contains

{
   "firstName": "Иван",
   "lastName": "Иванов",
   "address": {
       "streetAddress": "Московское ш., 101, кв.101",
       "city": "Ленинград",
       "postalCode": 101101
   },
   "phoneNumbers": [
       "812 123-1234",
       "916 123-4567"
   ]

}

in the end my开发者_Python百科 Memo2->Text

gets {}


If memory serves, this was not supported in C++Builder 2010 because DBXJSON uses Delphi's RTTI which was not correctly supported until C++Builder XE.


For C++Builder there is also JSonCBuilderBlog Library, it is free and open source.

Your source code can be translated in the following way:

TMetaObject obj;
obj.Decode(Memo1->Text);
Memo2->Text = obj.Encode();

As you can see the above code is very simple and it does not require any delete operator.

Considering your JSON source string, you can access to your data, after decoding it, in the following way:

UnicodeString FirstName = obj["firstName"];
UnicodeString LastName  = obj["lastName"];
UnicodeString Street    = obj["address"]["streetAddress"];
UnicodeString City      = obj["address"]["city"];
....

Moreover, the JSonCBuilderBlog Library is able to define new data type and makes a strong type checking on JSON source string. This is usefull to validate the data coherence based on the new data type defined by user.

For your reference following the library link: JSonCBuilderBlog Class Library.

0

精彩评论

暂无评论...
验证码 换一张
取 消