Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot use operator[] with number #499

Closed
brainmixxx opened this issue Mar 12, 2017 · 5 comments
Closed

cannot use operator[] with number #499

brainmixxx opened this issue Mar 12, 2017 · 5 comments

Comments

@brainmixxx
Copy link

Hello, I have unfortunately a problem with which I come no further:

I have data in the form of a list of multiple objects and I want to create json :: array's from it.

Input:
{ "candles":[ { "close":"470.0380", "epoch":1489306680, "high":"470.0954", "low":"469.8577", "open":"469.8577" }, { "close":"469.3634", "epoch":1489306740, "high":"470.0012", "low":"469.2554", "open":"470.0012" }, { "close":"469.4181", "epoch":1489306800, "high":"469.6009", "low":"469.1578", "open":"469.3178" } ], "echo_req": { "count":3, "end":"latest", "style":"candles", "ticks_history":"R_50" }, "msg_type":"candles" }

I would like the following output:
{ "open": {12, 21, 1}, "low": {23, 32, 2}, "high": {34, 43, 3} }

My approach is as follows:
json jCandles = { {"open", json::array()}, {"high", json::array()}, {"low", json::array()} }; for (unsigned int i = 0; i != jObj["candles"].size(); ++i) { jCandles["open"] += jObj["candles"][1]["open"]; }

i get following error:

terminate called after throwing an instance of 'std::domain_error' what(): cannot use operator[] with number

and with jCandles["open"].push_back(jObj["candles"][1]["open"]);
it says cannot use push_back() with number

I do not know how to handle the error. Can someone help me?

@nlohmann
Copy link
Owner

I'm not sure about the actual transformation of your JSON values, but I can compile your code without problems:

#include <json.hpp>

using json = nlohmann::json;

int main()
{
    json jObj = R"({ "candles":[ { "close":"470.0380", "epoch":1489306680, "high":"470.0954", "low":"469.8577", "open":"469.8577" }, { "close":"469.3634", "epoch":1489306740, "high":"470.0012", "low":"469.2554", "open":"470.0012" }, { "close":"469.4181", "epoch":1489306800, "high":"469.6009", "low":"469.1578", "open":"469.3178" } ], "echo_req": { "count":3, "end":"latest", "style":"candles", "ticks_history":"R_50" }, "msg_type":"candles" })"_json;

    json jCandles = {
        {"open", json::array()},
        {"high", json::array()},
        {"low", json::array()}
    };
    for (unsigned int i = 0; i != jObj["candles"].size(); ++i)
    {
        jCandles["open"] += jObj["candles"][1]["open"];
    }

    std::cout << std::setw(2) << jCandles << std::endl;
}

Output:

{
  "high": [],
  "low": [],
  "open": [
    "470.0012",
    "470.0012",
    "470.0012"
  ]
}

@brainmixxx
Copy link
Author

brainmixxx commented Mar 12, 2017

@nlohmann Thank you for the fast answer!
I think I've found the cause. I have built a class where I have created the list. Now I have the function outsourced and it works.

My class looks like this:

class candle_values {
public:
	json jCandles = {
		{"open", json::array()},
		{"high", json::array()},
		{"low", json::array()},
		{"close", json::array()},
		{"date", json::array()}
		};
	bool isNewCandle;`

	void new_list(json jObj){
		for (unsigned int i = 0; i != jObj["candles"].size(); ++i) {
			std::cout << "jC: " << jObj["candles"][i]["open"] << std::endl;
			jCandles["open"] += jObj["candles"][i]["open"];
		}
		std::cout << std::setw(2) << jCandles << std::endl;
		isNewCandle = false;
	}

Because of another class I work here with pointer and think that it must be because of it. Will check this today and write again.

@nlohmann
Copy link
Owner

Can I close this issue?

@brainmixxx
Copy link
Author

Yes sure! Many thanks for the help!

@nlohmann
Copy link
Owner

No worries!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants