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 get the json object again from which printed by the method of dump() #792

Closed
CoderOverflow opened this issue Oct 18, 2017 · 19 comments

Comments

@CoderOverflow
Copy link

CoderOverflow commented Oct 18, 2017

string wormhole;
wormhole = jsonPayload.dump();

How can I convert std::string to json object again?
When I use 'auto x = json::parse(wormhole.c_str()) ', the exception was occurred.

@CoderOverflow CoderOverflow changed the title how to get the json object again from which printed from dump() how to get the json object again from which printed by the method of dump() Oct 18, 2017
@nlohmann
Copy link
Owner

You don't need the .c_str(). Which exception did occur?

@CoderOverflow
Copy link
Author

CoderOverflow commented Oct 18, 2017

I found that the when there are some Chinese characters in json data, the parse function can't work.

@nlohmann
Copy link
Owner

We extensively tested the parse function with all kinds of UTF-8 characters, so it should work. Could you please provide an example where parsing fails?

@CoderOverflow
Copy link
Author

from dump string
["1","2017-10-18 17:38:41.000322","i","20160506","","晋A12345"] NG
["1","2017-10-18 17:37:12.000321","i","20160506","","a"] OK

@nlohmann
Copy link
Owner

Which exception do you get?

@CoderOverflow
Copy link
Author

I used "std::list c_list" to combine the string fields and used json ss(c_list) to create the ss json object, then run dump() method. as you can see the above results.
finally, I created a new json object using parse() to convert the dump string.

@nlohmann
Copy link
Owner

Which exception do you get? The exception gives hint where the parse error occurs.

@CoderOverflow
Copy link
Author

what kind of exception you want to catch? it just jump into the catch(...) block..

@nlohmann
Copy link
Owner

nlohmann commented Oct 18, 2017

Just surround the parse call with try/catch(std::exception &e) and give me the result of e.what().

@CoderOverflow
Copy link
Author

parse error - unexpected '"'

@nlohmann
Copy link
Owner

This is really strange. Could you please try without the .c_str()? Do you use the most recent version from the develop branch?

@CoderOverflow
Copy link
Author

c_list.push_back(W2A(strId));
								c_list.push_back(W2A(strDataTime));
								c_list.push_back("i");
								c_list.push_back(W2A(strRQ));
								c_list.push_back(W2A(strDDID));
								//c_list.push_back("a");
								c_list.push_back(W2A(strHP));
								json ss(c_list);
								std::string err = ss.dump();
								json xx = json::parse(err);

Actually, I didn't use c_str().

@nlohmann
Copy link
Owner

What does W2A do?

@CoderOverflow
Copy link
Author

how to check the version? I get the json.hpp at version 2.1.1 on 06/18/2017
W2A/A2W is the morco that will convert the between multiByte and wideChar
I use it to convert the CString of mfc to std::sting

@nlohmann
Copy link
Owner

Could you try this file: https://github.com/nlohmann/json/blob/develop/src/json.hpp

Is the resulting string UTF-8 encoded?

@CoderOverflow
Copy link
Author

I am not sure, maybe is not encoded by UTF-8. I will check this after.

@nlohmann
Copy link
Owner

Assuming strHP is the string that results in the failure, could you please try

std::string s = "\"";
s += W2A(strHP);
s += "\"";
json::parse(s);

@CoderOverflow
Copy link
Author

CoderOverflow commented Oct 18, 2017

It does the UTF-8 encoding cause this problem:)
I guess my the encoding of my environment in vs2015 is gbk.
After I added a coversion function converting string from gbk to utf-8, the exception was gone.

THX ^_^

@nlohmann
Copy link
Owner

Thanks for checking back!

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

No branches or pull requests

2 participants