Skip to content

newbie usage question #1063

Closed
Closed
@ghost

Description

Hello,

I was looking over your json framework and think that it is exactly what I need for my project, but in your documentation I did not see how to iterate over a json set of attributes.

For example if I have a json file that looks like this:

{
    "answers": [],
    "corrections": [],
    "infoboxes": [
        {
            "content": "...",
            "engine": "...",
            "id": "...",
            "img_src": "...",
            "infobox": "...",
            "urls": [
                {
                    "title": "...",
                    "url": "..."
                }
            ]
        }
    ],
    "number_of_results": ...,
    "query": "...",
    "results": [
        {
            "category": "....",
            "content": "...",
        },
        {
            "category": "....",
            "content": "...",
        },
        {
            "category": "....",
            "content": "...",
        },
        {
            "category": "....",
            "content": "...",
        },
        {
            "category": "....",
            "content": "...",
        }
    ],
    "suggestions": [],
    "somehting": [
        [
            "one",
            "two"
        ]
    ]
}

Then I have to 2 possible needs.

  1. I would read it in a string variable and want to iterate over the "category" and "content" attributes to collect all of the different information.

  2. I would like to be able to create the above json using your reference method.

In your example I would like to create:

{
  "pi": 3.141,
  "happy": true,
  "name": "Niels",
  "nothing": null,
  "answer": {
    "everything": 42
  },
  "list": [1, 0, 2],
  "object": {
    "currency": "USD",
    "value": 42.99
  },
  "object": {
    "currency": "USD",
    "value": 2.99
  },
  "object": {
    "currency": "USD",
    "value": 4.99
  }
}

You show:

// add another object (using an initializer list of pairs)
j["object"] = { {"currency", "USD"}, {"value", 42.99} };

but can it also be done something like:

// add another object (using an initializer list of pairs)
j["object"] ["currency"]=  "USD"
j["object"]["value"]= 42.99;

but that would only be for 1 object and I need to be able to add many "object" items to a structure.

I also need to be able to reference nested items in a similar way if possible.

Thanks,

Metadata

Assignees

No one assigned

    Labels

    solution: proposed fixa fix for the issue has been proposed and waits for confirmation

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions