-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Comments
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.
|
Hello,
What I have is a server returning some json with the following structure:
{
"answers": [],
"corrections": [],
"infoboxes": [
{
"content": "...",
"engine": "...",
"id": "...",
"img_src": "...",
"infobox": "...",
"urls": [
{
"title": "...",
"url": "..."
}
]
}
],
"number_of_results": 000,
"query": "...",
"results": [
{
"category": "...",
"content": "...",
"engine": "...",
"engines": [
"1",
"2",
"3"
],
"positions": [
1,
2,
1
],
"score": 7.5,
"title": "...",
"url": "..."
},
{
"category": "...",
"content": "...",
"engine": "...",
"engines": [
"1",
"2",
"3"
],
"positions": [
1,
2,
1
],
"score": 7.5,
"title": "...",
"url": "..."
},
{
"category": "...",
"content": "...",
"engine": "...",
"engines": [
"1",
"2",
"3"
],
"positions": [
1,
2,
1
],
"score": 7.5,
"title": "...",
"url": "..."
}
],
"suggestions": [],
"unresponsive_engines": [
[
"A",
"B"
],
[
"ONE",
"TWO"
]
]
}
And I am trying to find a way to read the data that is in each "category",
"content", "engine", "engines", "positions", etc.... as these are an array
of objects of the type:
{
"category": "...",
"content": "...",
"engine": "...",
"engines": [
"1",
"2",
"3"
],
"positions": [
1,
2,
1
],
"score": 7.5,
"title": "...",
"url": "..."
}
Any ideas on this as this is the structure that the server is returning to
me in the call?
Thanks again
…On Mon, Apr 23, 2018 at 2:48 AM, Niels Lohmann ***@***.***> wrote:
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
}
]
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#1063 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AXkxHgcjbieKNRZd_WjRsSvFV3QzhJceks5trXlWgaJpZM4Te8ke>
.
|
You would use the |
Thanks for the information and I will work on it.
Is there, by chance any examples for this?
My json data feed is received into an stl std::string or std::streamstring
type and I am mostly just looking to iterate through the array of objects
to get the list of items.
Thanks again,
…On Mon, Apr 23, 2018 at 7:57 AM, Niels Lohmann ***@***.***> wrote:
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.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#1063 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AXkxHkpTuou2IKgoCvG6JjRL5YjjamOxks5trcGSgaJpZM4Te8ke>
.
|
Do you need further assistance? |
Hi Niels,
I think that I am all set for the moment. We can close this ticket.
Thanks again,
…On Wed, Apr 25, 2018 at 4:42 PM, Niels Lohmann ***@***.***> wrote:
Do you need further assistance?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#1063 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AXkxHo1uRtQHM9oSBkRX8ccgXxIY9dDSks5tsN-9gaJpZM4Te8ke>
.
|
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:
Then I have to 2 possible needs.
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.
I would like to be able to create the above json using your reference method.
In your example I would like to create:
You show:
but can it also be done something like:
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,
The text was updated successfully, but these errors were encountered: