Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

Parsing JSON

Code
json object = bsoncxx::to_json(*maybe_result);

    // call find
    auto it_two = object.find("username");

    std::cout << object << std::endl;
    std::cout << std::boolalpha;
    std::cout << "username was found: " << (it_two != object.end()) << '\n';

I'm trying to find the value of the index called "username" so i only want the value it holds, which should be admin. But the problem is i cant parse it, it always returns false

how it looks:
http://prntscr.com/mgfweq

response: http://prntscr.com/mgfwj8

using https://github.com/nlohmann/json
(Feb 4, 2019 06:22 AM)int main() Wrote:  
Code
json object = bsoncxx::to_json(*maybe_result);

    // call find
    auto it_two = object.find("username");

    std::cout << object << std::endl;
    std::cout << std::boolalpha;
    std::cout << "username was found: " << (it_two != object.end()) << '\n';

I'm trying to find the value of the index called "username" so i only want the value it holds, which should be admin. But the problem is i cant parse it, it always returns false

how it looks:
http://prntscr.com/mgfweq

response: http://prntscr.com/mgfwj8

using https://github.com/nlohmann/json

Can you post the full code & json ? we can't help you without them due the fact that we can't know if you even read in the correct area..
Code
#include <nlohmann/json.hpp>
#include <iostream>

int main()
{
    nlohmann::json json_obj = "{\"username\":\"admin\"}"_json;
    auto it = json_obj.find("username");

    std::cout << json_obj << std::endl;
    std::cout << std::boolalpha;
    std::cout << "username was found: " << (it != json_obj.end()) << '\n';
    std::cout << "username: " << *it << '\n';
}

Works just fine for me. If there is an issue, it's with the result of your input json object...

Code
{"username":"admin"}
username was found: true
username: "admin"

There is a parse() method too. I haven't even really looked but I'd imagine that's what the literal overload resolves to.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(Feb 4, 2019 06:02 PM)0xDEAD10CC Wrote:  
Code
#include <nlohmann/json.hpp>
#include <iostream>

int main()
{
   nlohmann::json json_obj = "{\"username\":\"admin\"}"_json;
   auto it = json_obj.find("username");

   std::cout << json_obj << std::endl;
   std::cout << std::boolalpha;
   std::cout << "username was found: " << (it != json_obj.end()) << '\n';
   std::cout << "username: " << *it << '\n';
}

Works just fine for me. If there is an issue, it's with the result of your input json object...

Code
{"username":"admin"}
username was found: true
username: "admin"

There is a parse() method too. I haven't even really looked but I'd imagine that's what the literal overload resolves to.

Yep had to use the parse, ty!


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by roosiinfidel09May 26, 2021 06:32 AM 1 274 May 26, 2021 06:32 AM
Last Post: h0lyl337
[.NET] by outlawNov 28, 2020 04:25 AM 12 367 Nov 28, 2020 04:25 AM
Last Post: Rmdy
[C#] by HardStoNAug 15, 2020 03:56 AM 6 292 Aug 15, 2020 03:56 AM
Last Post: HardStoN