Hack Forums
[C] Parsing JSON - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C] Parsing JSON (/showthread.php?tid=5937474)



Parsing JSON - int main() - Feb 4, 2019

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


RE: Parsing JSON - Abruptum - Feb 4, 2019

(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..


RE: Parsing JSON - 0xDEAD10CC - Feb 4, 2019

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.


RE: Parsing JSON - int main() - Feb 4, 2019

(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!