-1

i wanna read Json file with c++, im using dev c++. Json file:

{
"product":{"title":"xxx","rating":4.5,"reviews_total":82,"image":"xxx","asin":"xxx","link":"xxx"},
"offers":[{
"price":{"symbol":"€","currency":"EUR","value":35.78,"raw":"35,78 €"},
"minimum_order_quantity":{"value":1},
"maximum_order_quantity":{"value":30},]
}

And i want to read Offers - Price - Value

My code now:

#include "json.hpp"
#include <fstream>
#include <iostream>
#include <sstream>

using namespace std;

int main()
{
    ifstream fJson("test.json");
    stringstream buffer;
    buffer << fJson.rdbuf();
    auto json = nlohmann::json::parse(buffer.str());

    cout << "\nPozostalo zapytan: " << json["request_info"]["credits_remaining"] << "\n";
    cout << "Asin: "<< json["request_parameters"]["asin"] <<endl;

    
    cout << "Cena produktu: "<< json["price"]["value"] <<endl;

    return 0;

}

showin Asin and credits_remaining works, but i cant get value from offers price, i try to

json["offers"]["price"]["value"]

i get

terminate called after throwing an instance of 'nlohmann::json_abi_v3_11_2::detail::type_error' what(): [json.exception.type_error.305] cannot use operator[] with a string argument with array

get help or answer :)

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
TheOwnIt
  • 1
  • 1

1 Answers1

-1
json["offers"][0]["price"]["value"]

Worked for me.

4b0
  • 21,981
  • 30
  • 95
  • 142
TheOwnIt
  • 1
  • 1