0

Is it possible to read the following data below using boost?

{
  "ok": true,
  "result": [
    {
      "update_id": 1235285,
      "message": {
        "message_id": 2,
        "from": {
          "id": 3325446,
          "is_bot": false,
          "first_name": "Test",
          "language_code": "en-PH"
        },
        "chat": {
          "id": 12532541,
          "first_name": "Test Account",
          "type": "private"
        },
        "date": 152014521,
        "text": "Test Message"
      }
    }
  ]
}
halfer
  • 19,824
  • 17
  • 99
  • 186
batch 1999
  • 109
  • 9
  • 1
    Look at boost::property_tree – ForEveR Sep 05 '17 at 12:19
  • Possible duplicate of [Reading json file with boost](https://stackoverflow.com/questions/15206705/reading-json-file-with-boost) – Andre Kampling Sep 05 '17 at 12:24
  • Don't, boost::property_tree is very ugly and buggy. – Arpegius Sep 05 '17 at 12:25
  • @AndreKampling data is stored on std::string. It is not stored on file – batch 1999 Sep 05 '17 at 12:27
  • @batch1999: Read the solution in the thread. It uses `std::stringstream`, so he can read a file or a string... – Andre Kampling Sep 05 '17 at 12:28
  • @AndreKampling i already done that but no success – batch 1999 Sep 05 '17 at 12:36
  • @batch1999: It would be helpful to ask more specific. What are the problems, errors, ...? – Andre Kampling Sep 05 '17 at 12:38
  • i realy dont where the errors are because i am trying to show the data using MessageBox but the error shows on different class not on the code. I have tried the code on the link that you give. – batch 1999 Sep 05 '17 at 12:39
  • 1
    @Arpegius That's bullshit. Boost Property is not ugly and certainly not buggy. What you mean is that it's not a general purpose JSON library. It's a property tree library. \[Apparently wishful thinking causes people to assume it's a JSON and an XML library. None of which is true, obviously]. If you don't need that, use a JSON library, really. But If property tree does fit the bill, it's fine. [Limitations are worded in all of the 12 lines of documentation for the JSON backend](http://www.boost.org/doc/libs/1_65_0/doc/html/property_tree/parsers.html#property_tree.parsers.json_parser). – sehe Sep 05 '17 at 21:51

1 Answers1

1

You could see the linked post in comment,

To summarize you can have like following to read from a file say mfile.json :

    boost::property_tree::ptree pt;
    boost::property_tree::read_json("myfile.json", pt);

    print_contents( pt );

where print_contents is:

void print_contents( const boost::property_tree::ptree& pt)
{
    using boost::property_tree::ptree;

    for (const auto& x: pt )
    {
        std::cout << x.first << ": " << x.second.get_value<std::string>() << std::endl;
        print_contents(x.second);
    }
}

See Here


I could have closed it as duplicate, but looks like there was no "better" post for reading a json file

P0W
  • 46,614
  • 9
  • 72
  • 119
  • there was an error on `for(const auto& x:pt)` where x is the error – batch 1999 Sep 05 '17 at 12:51
  • 1
    @batch1999 see the linked demo. `x` is variable used in the range based for loop. Make sure you enable C++11 flag – P0W Sep 05 '17 at 12:53
  • `void TestBotDlg::OnConnectWebserver() { tgapi bot(BOT_ID); string tmp = bot.getUpdates(); stringstream ss; ss<()); MessageBox(temp); } }` – batch 1999 Sep 05 '17 at 12:57
  • 2
    @batch1999 you're being a [help vampire](http://www.skidmore.edu/~pdwyer/e/eoc/help_vampire.htm). Ask a proper question. (The answer to your question now is obviously "Yes"). Edit your question to include a [SSCCE](http://sscce.org/) and the expected outcome. – sehe Sep 05 '17 at 21:57
  • @sehe: This made my day! A lot of people are help vampires and you can meet a lot of them here on SO. Now I know this awesome term that hits the nail. Thank you! – Andre Kampling Sep 06 '17 at 06:59
  • but how can i implement on mfc application? – batch 1999 Sep 06 '17 at 09:00
  • @batch1999 I gave you a code. Which you can **put/use** anywhere in C++ project, provided you have right library and right compiler. – P0W Sep 06 '17 at 09:05