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

How to create a json object from a std::string, created by j.dump? #1645

Closed
antithing opened this issue Jun 17, 2019 · 8 comments
Closed

How to create a json object from a std::string, created by j.dump? #1645

antithing opened this issue Jun 17, 2019 · 8 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@antithing
Copy link

Hi, thank you very much for this library.

I am sending a json over UDP.

I create it like this:

json j = {
  {"pi", 3.141},
  {"happy", true},
  {"name", "Niels"},
  {"nothing", nullptr},
  {"answer", {
    {"everything", 42}
  }},
  {"list", {1, 0, 2}},
  {"object", {
    {"currency", "USD"},
    {"value", 42.99}
  }}
};

I dump it to a string:

std::string s = j.dump();

and send it:

client.send(s);

When I receive it, it's as a std::string.

std::string data_;
It looks correct.

Now, i want to create a json object from this string that matches the one i sent.
json j2 = data_;

How can i do this?

Thank you.

@nickaein
Copy link
Contributor

As stated in Serialization / Deserialization section of README, you can use json::parse().

@antithing
Copy link
Author

Thanks for getting back to me.
from:

std::cout << data_ << std::endl;	
json j;
j.parse(data_);

I am getting this:

{"recording":true,"slate":"201900416_demodemo_test01","take number":14,"timecode":[12,12,12,17]}

[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal

Should I be checking that the string is valid somehow?

Thanks again

@nickaein
Copy link
Contributor

As demonstrated in README, you should use the static parse() method:

auto j3 = json::parse("{ \"happy\": true, \"pi\": 3.141 }");

Nevertheless, the error you mentioned indicates that the string is empty. You should make sure it doesn't contains invalid characters (e.g. 0 byte at the beginning). Printing out the first bytes of string as HEX might be helpful.

@antithing
Copy link
Author

thank you! one more quick thing...

My string is:

{"recording":true,"slate":"201900416_demodemo_test01","take number":14,"timecode":[12,12,12,21]}
I am using:

	
			for (json::iterator it = j.begin(); it != j.end(); ++it)
			{
				
				if (it.key() == "recording")
				{
					if (it.value() == "true")
					{
						recording = true;
					}
					if (it.value() == "false")
					{
						recording = false;
					}

					std::cout << it.key() << " : " << it.value() << "\n";
				}
				
				if (it.key() == "slate")
				{
					Slate = it.value();
					std::cout << it.key() << " : " << it.value() << "\n";
				}

				if (it.key() == "timecode")
				{

					timecode = it.value();
					std::cout << it.key() << " : " << it.value() << "\n";
				}
								
			}

and i get:

recording : true
slate : "201900416_demodemo_test01"
[json.exception.type_error.302] type must be string, but is array

What is the approach to get the array from a json like this?

Thanks!

@nickaein
Copy link
Contributor

Maybe because timecode is an array in the JSON?

You should prefer explicit type conversion. e.g.:

auto timecode = json["timecode"].get<std::vector<int>>();

Also, you don't have to iterate over the keys for retriering their values. You can directly access them (just like above). There is also APIs to check for presence of a key (e.g. find() or contains(). Have a look at Examples section of README for code examples and guidelines on usage.

@antithing
Copy link
Author

ah that is a much nicer way to do it! Thank you very much for your time.

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Jun 29, 2019
@BrandonLiang
Copy link

As demonstrated in README, you should use the static parse() method:

auto j3 = json::parse("{ \"happy\": true, \"pi\": 3.141 }");

Nevertheless, the error you mentioned indicates that the string is empty. You should make sure it doesn't contains invalid characters (e.g. 0 byte at the beginning). Printing out the first bytes of string as HEX might be helpful.

What is the difference between the non-static parse() and static parse()? In other words, when should we use non-static parse() as opposed to the static parse()?

@nlohmann
Copy link
Owner

There is no nonstatic parse method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

4 participants