Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

newbie usage question #1063

Closed
Outstep opened this issue Apr 22, 2018 · 7 comments
Closed

newbie usage question #1063

Outstep opened this issue Apr 22, 2018 · 7 comments
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@Outstep
Copy link

Outstep commented Apr 22, 2018

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,

@nlohmann
Copy link
Owner

You cannot create a JSON like

{
  "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
  }
}

because each key in an object must be unique. See also https://nlohmann.github.io/json/classnlohmann_1_1basic__json_a5e48a7893520e1314bf0c9723e26ea2a.html#a5e48a7893520e1314bf0c9723e26ea2a (section "Behavior").

You could instead create an array of objects, e.g.

[
{
    "currency": "USD",
    "value": 4.99
},
{
    "currency": "USD",
    "value": 2.99
}
]

@Outstep
Copy link
Author

Outstep commented Apr 23, 2018 via email

@nlohmann
Copy link
Owner

You would use the operator[] in most cases, as described in https://github.com/nlohmann/json#json-as-first-class-data-type. Iteration is done similar to any other STL container.

@Outstep
Copy link
Author

Outstep commented Apr 23, 2018 via email

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Apr 23, 2018
@nlohmann
Copy link
Owner

Do you need further assistance?

@Outstep
Copy link
Author

Outstep commented Apr 25, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

1 participant