Thread: Getting A Specific Item With JsonCpp

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    139

    Getting A Specific Item With JsonCpp

    Hello,

    Can anyone please tell me how I can access the value of menu.item1.element1-1 (which would be "value1-1" with the jsoncpp library. For some reason I dont seem to be able to get that value.


    // here is the json
    Code:
    
    "menu":{
          "item1":{
         "element1-1":"value1-1",
         "element1-2":"value1-2",
         "element1-3":"value1-3"
          },
          "item2":{
         "element2-1":"value2-1",
         "element2-2":"value2-2",
         "element2-3":"value2-3"
          },
       }
    }
    



    //here is the cpp code
    Code:
    Json::Value root;
    Json::Reader reader;
    constJson::Value defValue;
    std::ifstream ifile("inputjson.txt");
    
    
    bool isJsonOK =( ifile != NULL && reader.parse(ifile, root));
    if(isJsonOK)
    {
        Json::Value val = root.get("menu","");
        for(Json::Value::iterator it = val.begin(); it != val.end();++it)
        {
             Json::Value key = it.key();
             Json::Value value =(*it);
    
    
             // how do i get the details of 'menuValue'
             const Json::Value menuValue = root["menu"]["item1"];
    
    
       }
    }

    Last edited by EverydayDiesel; 05-08-2016 at 08:16 PM.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Your json is missing the first brace (before "menu"), but I assume you know that.
    Is this what you want:
    Code:
        Json::Value v = root["menu"]["item1"]["element1-1"];
        std::cout << v.asString() << std::endl;

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    139
    awesome, thank you for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. item storage
    By a29bbas in forum C Programming
    Replies: 1
    Last Post: 12-30-2013, 11:40 AM
  2. How to display item at specific tab position
    By yeefc128 in forum C++ Programming
    Replies: 4
    Last Post: 11-27-2013, 09:16 PM
  3. Replies: 6
    Last Post: 07-10-2006, 12:05 AM
  4. searching for a specific item in a C++ file
    By stanleyw in forum C++ Programming
    Replies: 1
    Last Post: 06-03-2002, 04:54 PM
  5. Delete Item
    By emilyh in forum Windows Programming
    Replies: 10
    Last Post: 10-03-2001, 09:33 PM

Tags for this Thread