write arrayvalue

2007-11-22
2013-05-07
  • Younggi Lim

    Younggi Lim - 2007-11-22

    I know that write StringValue and IntegerValue.
    {
    "account" : "javawork",
    "serial" : 10
    }

    Json::Value writeValue;
    writeValue["account"] = std::string("javawork");
    writeValue["serial"] = 10;

    but How do I write ArrayValue?
    {
    "friends":
        [
          "alice",
          "rabbit",
          "queen"
        ]
    }

     
    • venkat gunnu

      venkat gunnu - 2008-04-28

      Hey Buddy!

      By Any chance did u get this working. I am stuck with the same problem. Any help would be great.

      Thanks
      venkat

       
    • venkat gunnu

      venkat gunnu - 2008-04-30

      Some one might be interested.

      Json::Value jValueRoot;
      Json::Reader jReader;
      _bstr_t sss = _T("[]");
      if(jReader.parse((char *)(sss), jValueRoot))
      {
         if(jValueRoot.isArray()){
            Json::Value widgetMem;
            widgetMem["name"] = 'name';
            Json::Value::UInt ii = jValueRoot.size();
            jValueRoot[ii] = widgetMem;
            Json::StyledWriter writer;
            std::string output = writer.write( jValueRoot );
         }
      }

      Thanks
      venkat

       
    • invisigoth

      invisigoth - 2009-04-20

      I struggled a lot to do dis, but its actually quite easy :-)

          Json::FastWriter w;
          Json::Value root;

          Json::Value::UInt index=0;
          root["Features"][index]["test"]="null";

       
  • Alexander Beedie

    i had a momentary pause on this point too, but it's actually trivial.
    use ".append" on the node you want to add an array to.

    Json::Value friend_node = root["friends"];
    friend_node.append("alice");
    friend_node.append("rabbit");
    friend_node.append("queen");
    

    makes it nice & easy to add arrays via looping, etc. you can also create 2D arrays (eg: arrays of arrays) easily in the same way, by looping appends onto the first/second/third/etc element in the array you just created.

     
  • Barbu Paul - Gheorghe

    I know it's an old thread, but shouldn't be it:

    Json::Value friend_node;
    friend_node.append("alice");
    friend_node.append("rabbit");
    friend_node.append("queen");
    root["friends"] = friend_node;
    
     
    • henshao

      henshao - 2013-05-07

      Json::Value& friend_node = root["friends"];
      friend_node.append("alice");
      friend_node.append("rabbit");
      friend_node.append("queen");

      maybe better.

       
  • Anonymous - 2012-08-21

    Thanks Paulik , your solution got me working.

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks