6

For example:

    Json::Reader reader;
    Json::Value val;
    ifstream file("JSON/test.json");
    bool success = reader.parse(file, val, false);
    vector<string> obj = val.getMemberNames();

    for (int i = 0; i < val.size(); i++)
    {
        // switch type of value
        switch (val.get(obj.at(i), "default").type())
        {
            case stringValue:
                cout << "I'm string" << endl;
                ... need to save **membername** and **value**
                break;
            case intValue:
                cout << "I'm int"  << endl;
                ... need to save **membername** and **value**
                break;
            case nullValue:
                cout << "I'm null"  << endl;
                break;
            case arrayValue:
                 ... code to parse an array (with nested sure) ...
                 ... need to save
                break;
            case objectValue:
                ... code to parse an object (with nested sure) ...
                ... need to save 
                break;
        }
    }

Example JSON file (JSON/test.json):

{
    "layout": "fit",
    "xtype": "viewport",
    "height": 200,
    "style": {
                "backgroundColor": "46f0a8"
             },

           "items": 
           [
             {
                "title": "Management Console",
                "padding": "10 10 10 10",
                "bodyPadding": 10,
                "autoScroll": true,
                "items2": [1, 2, 3]
             }
           ]
 }

In array items, we can see another array (nested) called items2.

May experience the following for arrays:

array[Jacob, Joseph] or
array[{name: Jacob, name: Joseph}] or
array[{name: Jacob}, {name: Joseph}]

For objects it's similar. In each field, can of course be many nested objects/arrays. How do I save data in C++ variables?

2
  • It is quite unclear to me, what are you really asking for. I see 3 questions. How do I process array data and object data in jsoncpp library? How do I adapt to different data storing conventions? How do I handle general/nested cases? Commented Apr 15, 2014 at 11:50
  • It is unclear whether you want to parse into a predefined C++ class, or you want to parse arbitrary JSON data. For the latter case, there is really no point converting JSON::Value into other formats; just use JSON::Value directly. Commented Nov 12, 2014 at 2:23

1 Answer 1

0

You'll want to make your main parsing function recursive. Then use STL classes like vector that allow you to dynamically allocate more storage for your data.

Sign up to request clarification or add additional context in comments.

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.