0

I have yahoo finance json file from which I want to isolate Date,Close and volume from the quote list and save it in the same order with a comma separtion in a single text file. This is my json script.

Json::Value root;   // will contains the root value after parsing.
Json::Reader reader;
bool parsingSuccessful = reader.parse( YahooJson, root );
if(not parsingSuccessful)
 {
   // Report failures and their locations
   // in the document.
   std::cout<<"Failed to parse JSON"<<std::endl
       <<reader.getFormatedErrorMessages()
       <<std::endl;
   return 1;
 }else{
 std::cout<<"\nSucess parsing json\n"<<std::endl;
 std::cout << root<< std::endl;
 std::cout <<"No of Days = "<< root["query"]["count"].asInt() << std::endl;

//below for loop returns an error
 for (auto itr : root["query"]["result"]["quote"]) {
    std::string val = itr.asString();

} 


 }

I was able to succed in fetching the json values and print root["query"]["count"].asInt() but when I go to the list values(quote) I dont know how to iterate through quote (query->result->quote) to get Date,close and volume values?

EDIT

Also tried this method

const Json::Value& quotes = root["query"]["results"]["quote"];
for (int i = 0; i < quotes.size(); i++){
    std::cout << "    Date: " << quotes[i]["Date"].asString();
    std::cout << "   Close: " << quotes[i]["Close"].asFloat();
    std::cout << "  Volume: " << quotes[i]["Volume"].asFloat();
    std::cout << std::endl;
}

It works only when output was Date. For close and volume output it exits with a runtime error message and also this error

what() type is not convertible to string
Eka
  • 14,170
  • 38
  • 128
  • 212
  • Can you clarify which JSON library you are using? What library are you importing that defines `Json::Value` and `Json::Reader`? It would also help if you post a snippet of the JSON record you are trying to access so that it's clear whether it is a JSON array or something else. – Evan VanderZee Jul 23 '16 at 11:57
  • 1
    When I try your edited second method, I get `what(): Value is not convertible to float.` This makes sense because the data for Close and Volume is presented as strings rather than numeric literals in the linked sample JSON data. There are reasons related to rounding that you might prefer to parse it a different way. If doubles are okay, you should be able to convert it from a string using scanf or a string stream. – Evan VanderZee Jul 24 '16 at 01:54

1 Answers1

1

You haven't specified which JSON library you are using, and I don't know the Yahoo finance data well enough to know the exact field names, but if you are using the JsonCpp library, which has documentation here, and you are asking about how to iterate over a JSON array, then one way to do it using iterators would look something like this

const Json::Value quote = root["query"]["results"]["quote"];
for (Json::ValueConstIterator itr = quote.begin(); itr != quote.end(); ++itr)
{
  const Json::Value date = (*itr)["Date"];
  const Json::Value close = (*itr)["Close"];
  const Json::Value volume = (*itr)["Volume"];
  std::cout << "Date: " << date.asString() << std::endl;
  std::cout << "Close: " << close.asString() << std::endl;
  std::cout << "Volume: " << volume.asString() << std::endl;
}
Evan VanderZee
  • 797
  • 6
  • 10
  • Thanks for the code after minor correction also its not showing any result `Json::Value quote = root["query"]["result"]["quote"]; for (Json::ValueIterator itr = quote.begin() ; itr != quote.end() ; itr++) { Json::Value date= (*itr)["Date"]; Json::Value close= (*itr)["Close"]; Json::Value volume = (*itr)["Volume"]; std::cout<<"\nDate\n"< – Eka Jul 23 '16 at 14:49
  • I had not compiled the code previously, but I went to the work of compiling it due to your comment. The answer now shows code that I have compiled and tested with the JsonCpp code and the data shown in the yahoo finance json link you provided. – Evan VanderZee Jul 24 '16 at 01:47