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

How to get value from array #1762

Closed
ra1nst0rm3d opened this issue Sep 22, 2019 · 11 comments
Closed

How to get value from array #1762

ra1nst0rm3d opened this issue Sep 22, 2019 · 11 comments
Labels
kind: question state: needs more info the author of the issue needs to provide more details

Comments

@ra1nst0rm3d
Copy link

ra1nst0rm3d commented Sep 22, 2019

  • I want to get a value of 'id' from array items

I'm give this JSON code:

{
    "response": {
        "count": 246,
        "items": [
            {
                "id": 456239356,
                "album_id": -15,
                "owner_id": 223134400,
                "sizes": [
                    {
                        "type": "m",
                        "url": "https://sun9-25.u...4c5/FPhcIYb3UH0.jpg",
                        "width": 97,
                        "height": 130
                    },
                    {
                        "type": "o",
                        "url": "https://sun9-41.u...4c7/JDqz1u_L8p4.jpg",
                        "width": 130,
                        "height": 173
                    },
                    {
                        "type": "p",
                        "url": "https://sun9-61.u...4c8/DB5DZsOldK4.jpg",
                        "width": 200,
                        "height": 267
                    },
                    {
                        "type": "q",
                        "url": "https://sun9-4.us...4c9/ckrBy4eD8-c.jpg",
                        "width": 320,
                        "height": 427
                    },
                    {
                        "type": "r",
                        "url": "https://sun9-10.u...4ca/YMxebK1MdXE.jpg",
                        "width": 450,
                        "height": 600
                    },
                    {
                        "type": "s",
                        "url": "https://sun9-14.u...4c4/3SBIzASVRfg.jpg",
                        "width": 56,
                        "height": 75
                    },
                    {
                        "type": "x",
                        "url": "https://sun9-43.u...4c6/M2kNQnDOHEU.jpg",
                        "width": 450,
                        "height": 600
                    }
                ],
                "text": "",
                "date": 1531926387
            },
            {
                "id": 456239361,
                "album_id": -15,
                "owner_id": 223134400,
                "sizes": [
                    {
                        "type": "m",
                        "url": "https://sun9-47.u...1b6/diTkAYuSeEY.jpg",
                        "width": 97,
                        "height": 130
                    },
                    {
                        "type": "o",
                        "url": "https://sun9-5.us...1bb/UJd8Jlha1Ow.jpg",
                        "width": 130,
                        "height": 173
                    },
                    {
                        "type": "p",
                        "url": "https://sun9-49.u...1bc/FF8SEu_oBt0.jpg",
                        "width": 200,
                        "height": 267
                    },
                    {
                        "type": "q",
                        "url": "https://sun9-47.u...1bd/hXZmMKhYgFw.jpg",
                        "width": 320,
                        "height": 427
                    },
                    {
                        "type": "r",
                        "url": "https://sun9-37.u...1be/dcyHZoEEfBQ.jpg",
                        "width": 510,
                        "height": 680
                    },
                    {
                        "type": "s",
                        "url": "https://sun9-28.u...1b5/hW5bO1WXZ04.jpg",
                        "width": 56,
                        "height": 75
                    },
                    {
                        "type": "w",
                        "url": "https://sun9-31.u...1ba/m3ZFNMnSdUg.jpg",
                        "width": 1620,
                        "height": 2160
                    },
                    {
                        "type": "x",
                        "url": "https://sun9-54.u...1b7/Tzx4N7wD3YE.jpg",
                        "width": 453,
                        "height": 604
                    },
                    {
                        "type": "y",
                        "url": "https://sun9-11.u...1b8/dhmIRt3eZ8c.jpg",
                        "width": 605,
                        "height": 807
                    },
                    {
                        "type": "z",
                        "url": "https://sun9-37.u...1b9/3iavgJV9YWQ.jpg",
                        "width": 810,
                        "height": 1080
                    }
                ],
                "text": "",
                "date": 1531935090
            }
        ]
    }
}
  • Arch Linux, CLang 8

  • Version of library 2.1.1

@vega0
Copy link

vega0 commented Sep 23, 2019

same interesting

@vega0
Copy link

vega0 commented Sep 23, 2019

Try to
json j = json::parse("your-json-here");
int id = j["response"]["items"][0]["id"];

@nlohmann
Copy link
Owner

This should work:

json j = json::parse("your_file.json");

for (const auto& item : j["response"])
{
    int id = item["id"];
}

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Sep 23, 2019
@ra1nst0rm3d
Copy link
Author

I want give EVERY id value in massive

@ra1nst0rm3d
Copy link
Author

@nlohmann, your code doesn't work

@nlohmann
Copy link
Owner

My bad:

  • You need to first create an std::ifstream with your file and pass it to std::parse.
  • The for loop should be for (const auto& item : j["response"]["items"]).

@ra1nst0rm3d
Copy link
Author

ra1nst0rm3d commented Sep 23, 2019

@nlohmann, caught a logic_error exception

@nlohmann
Copy link
Owner

Do you have a stack trace? I need more information.

@ra1nst0rm3d
Copy link
Author

(gdb) n [New Thread 0x7ffff6e9d700 (LWP 10480)] [Thread 0x7ffff6e9d700 (LWP 10480) exited] 46 for (const auto& item : second["response"]["items"]) (gdb) n 46 for (const auto& item : second["response"]["items"]) (gdb) n 54 }; (gdb) n 54 }; (gdb) n 54 }; (gdb) n 0x00007ffff7a31ee3 in __libc_start_main () from /usr/lib/libc.so.6 (gdb) n Single stepping until exit from function __libc_start_main, which has no line number information. [Inferior 1 (process 10474) exited normally] (gdb)

@ra1nst0rm3d
Copy link
Author

@nlohmann, i don't know how to format code on GitHub

@nlohmann
Copy link
Owner

In order to asses your issue, we need the following information:

  • What is the issue you have?

  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?

  • What is the expected behavior?

  • And what is the actual behavior instead?

  • Which compiler and operating system are you using? Is it a supported compiler?

  • Did you use a released version of the library or the version from the develop branch?

  • If you experience a compilation error: can you compile and run the unit tests?

@nlohmann nlohmann added state: needs more info the author of the issue needs to provide more details and removed solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Sep 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question state: needs more info the author of the issue needs to provide more details
Projects
None yet
Development

No branches or pull requests

4 participants
@nlohmann @vega0 @ra1nst0rm3d and others