CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2008
    Posts
    38

    help using arrays with libJSON

    Is there anyone here who's experienced at using libjson who could give me a quick example by any chance? say i've got the following code
    Code:
    JSONNode root, child;
    child.push_back(JSONNode("list", "array of strings goes here"));
    child.push_back(JSONNode("access", 126353359));
    child.set_name("root");
    root.push_back(child);
    I know it's extremely simplistic, it's just to give an example of what i'm trying to achieve. What I want to do is have an array of strings as a child node to 'list' but can't work out how to do this... Any help would be much appreciated...

  2. #2
    Join Date
    Jan 2009
    Posts
    1,689

    Re: help using arrays with libJSON

    Well, the way that you declare your list node:
    Code:
    child.push_back(JSONNode("list", "array of strings goes here"));
    is making it a string.

    Try this instead.
    Code:
    JSONNode list = JSONNode(JSON_ARRAY);  //declare it as an array
    list.set_name("list");  //name it "list"
    child.push_back(list); //push it onto the child
    
    ...
    
    //Now you can push strings into your list
    JSONNode & array = child["list"];  //get a reference to the list
    array.push_back(JSONNode("", "I am a string"));  //no name because arrays don't have them
    You can also populate your list before pushing it onto child, which is actually better. Because of reference counting, copying and pushing nodes is cheap.

    I'm actually the maintainer and author of libjson so this is a good place to get help if you need it. I also do watch the libjson forum on sourceforge
    Last edited by ninja9578; December 10th, 2010 at 10:41 AM.

  3. #3
    Join Date
    May 2008
    Posts
    38

    Re: help using arrays with libJSON

    ahh yeah i get it now, then we just
    Code:
    list.push_back(JSONNode("", "whatever"));
    to populate our array. Brilliant, thanks for that... Quickest reply ever. I did see your username floating around various places when I was sifting through google beforehand looking for an answer as it happends so I hoped if I posted here you'd answer Nice one

    edit: just saw your edit on setting up the list beforehand, thanks for the example I'll probably go down that road.

  4. #4
    Join Date
    Jan 2009
    Posts
    1,689

    Re: help using arrays with libJSON

    No problem, I check this forum every day as well as sourceforge if you need any more help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured