Add values to map using rapidjson
up vote
0
down vote
favorite
I get a raw json string
{"vehicle": {"brand": "zonda","color": "blue"},"username": {"brand": "doyota","color": "red"}}
from a get call i make.
I read that rapidjson is the best way to parse a json string in cpp.
So I tried doing something like this:
const char* json = data.c_str();
rapidjson::Document document;
if (document.Parse(json).HasParseError()) {
cout << "has parse error" << endl;
return 1;
}
else {
assert(document.IsObject());
}
Here it says that the json has a parse error. Any idea why this could be?
Also once I am able to parse the values I want to add them as key value pairs to a standard map. Could anyone point me in the right direction to proceed with this?
c++ json stdmap rapidjson
add a comment |
up vote
0
down vote
favorite
I get a raw json string
{"vehicle": {"brand": "zonda","color": "blue"},"username": {"brand": "doyota","color": "red"}}
from a get call i make.
I read that rapidjson is the best way to parse a json string in cpp.
So I tried doing something like this:
const char* json = data.c_str();
rapidjson::Document document;
if (document.Parse(json).HasParseError()) {
cout << "has parse error" << endl;
return 1;
}
else {
assert(document.IsObject());
}
Here it says that the json has a parse error. Any idea why this could be?
Also once I am able to parse the values I want to add them as key value pairs to a standard map. Could anyone point me in the right direction to proceed with this?
c++ json stdmap rapidjson
RapidJSON allows you to check what the error is. Have you tried looking at the specific error code? The list is pretty extensive: rapidjson.org/md_doc_dom.html#ParseError
– ahota
Nov 19 at 21:35
As far as the map is concerned, I've been working on something similar recently. The closest I found to a true JSON (and Python dictionary) is anstd::unordered_map
with a key typestd::string
and a value type ofstd::variant<>
, where the variant could be int, float, string, vector, etc.std::variant
requires C++17, though.
– ahota
Nov 19 at 21:39
@ahota the error says "invalid values". Although i suspect this could be because I am converting a standard string to const char*? Also i am restricted to cpp 11
– HAL9000
Nov 19 at 21:47
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I get a raw json string
{"vehicle": {"brand": "zonda","color": "blue"},"username": {"brand": "doyota","color": "red"}}
from a get call i make.
I read that rapidjson is the best way to parse a json string in cpp.
So I tried doing something like this:
const char* json = data.c_str();
rapidjson::Document document;
if (document.Parse(json).HasParseError()) {
cout << "has parse error" << endl;
return 1;
}
else {
assert(document.IsObject());
}
Here it says that the json has a parse error. Any idea why this could be?
Also once I am able to parse the values I want to add them as key value pairs to a standard map. Could anyone point me in the right direction to proceed with this?
c++ json stdmap rapidjson
I get a raw json string
{"vehicle": {"brand": "zonda","color": "blue"},"username": {"brand": "doyota","color": "red"}}
from a get call i make.
I read that rapidjson is the best way to parse a json string in cpp.
So I tried doing something like this:
const char* json = data.c_str();
rapidjson::Document document;
if (document.Parse(json).HasParseError()) {
cout << "has parse error" << endl;
return 1;
}
else {
assert(document.IsObject());
}
Here it says that the json has a parse error. Any idea why this could be?
Also once I am able to parse the values I want to add them as key value pairs to a standard map. Could anyone point me in the right direction to proceed with this?
c++ json stdmap rapidjson
c++ json stdmap rapidjson
asked Nov 19 at 21:18
HAL9000
385
385
RapidJSON allows you to check what the error is. Have you tried looking at the specific error code? The list is pretty extensive: rapidjson.org/md_doc_dom.html#ParseError
– ahota
Nov 19 at 21:35
As far as the map is concerned, I've been working on something similar recently. The closest I found to a true JSON (and Python dictionary) is anstd::unordered_map
with a key typestd::string
and a value type ofstd::variant<>
, where the variant could be int, float, string, vector, etc.std::variant
requires C++17, though.
– ahota
Nov 19 at 21:39
@ahota the error says "invalid values". Although i suspect this could be because I am converting a standard string to const char*? Also i am restricted to cpp 11
– HAL9000
Nov 19 at 21:47
add a comment |
RapidJSON allows you to check what the error is. Have you tried looking at the specific error code? The list is pretty extensive: rapidjson.org/md_doc_dom.html#ParseError
– ahota
Nov 19 at 21:35
As far as the map is concerned, I've been working on something similar recently. The closest I found to a true JSON (and Python dictionary) is anstd::unordered_map
with a key typestd::string
and a value type ofstd::variant<>
, where the variant could be int, float, string, vector, etc.std::variant
requires C++17, though.
– ahota
Nov 19 at 21:39
@ahota the error says "invalid values". Although i suspect this could be because I am converting a standard string to const char*? Also i am restricted to cpp 11
– HAL9000
Nov 19 at 21:47
RapidJSON allows you to check what the error is. Have you tried looking at the specific error code? The list is pretty extensive: rapidjson.org/md_doc_dom.html#ParseError
– ahota
Nov 19 at 21:35
RapidJSON allows you to check what the error is. Have you tried looking at the specific error code? The list is pretty extensive: rapidjson.org/md_doc_dom.html#ParseError
– ahota
Nov 19 at 21:35
As far as the map is concerned, I've been working on something similar recently. The closest I found to a true JSON (and Python dictionary) is an
std::unordered_map
with a key type std::string
and a value type of std::variant<>
, where the variant could be int, float, string, vector, etc. std::variant
requires C++17, though.– ahota
Nov 19 at 21:39
As far as the map is concerned, I've been working on something similar recently. The closest I found to a true JSON (and Python dictionary) is an
std::unordered_map
with a key type std::string
and a value type of std::variant<>
, where the variant could be int, float, string, vector, etc. std::variant
requires C++17, though.– ahota
Nov 19 at 21:39
@ahota the error says "invalid values". Although i suspect this could be because I am converting a standard string to const char*? Also i am restricted to cpp 11
– HAL9000
Nov 19 at 21:47
@ahota the error says "invalid values". Although i suspect this could be because I am converting a standard string to const char*? Also i am restricted to cpp 11
– HAL9000
Nov 19 at 21:47
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
This gave me no error:
#include <iostream>
#include "rapidjson/document.h"
#include "rapidjson/error/en.h"
using namespace rapidjson;
int main() {
Document d;
std::string json = R"raw({"vehicle": {"brand": "zonda","color": "blue"},"username": {"brand": "doyota","color": "red"}})raw";
if (d.Parse(json.c_str()).HasParseError()) {
std::cout << "has errorn";
} else {
std::cout << "no errorn";
}
}
Tried C++11 - C++20 and it all seems fine. My guess is that you've got some non UTF8 character in the data.
Oh nice this worked for me too. I there a significance to the R and the prefixed and suffixed raw here? Thing is, I get the json string from a get call. {"vehicle": {"brand": "zonda","color": "blue"},"username": {"brand": "doyota","color": "red"}} I don't actually have this in the code. can I append R or "raw" like we do in java? I am kinda cpp newbie so out of my depth here.
– HAL9000
Nov 19 at 22:07
TheR"delimiter( raw_characters )delimiter"
only does you good with string literals so my guess is that you've got some non UTF8 character in the data but it's hard to say. But at least you know the parser is working. The string you get, can you save it in binary mode to a file and then perhaps validate it so that it is UTF8 encoded?
– Ted Lyngmo
Nov 19 at 22:18
That was it! I was being rather stupid and returning VERBOSE and HEADER in libcurl get call. With just the JSON string in the body it works correctly!
– HAL9000
Nov 19 at 22:50
Have you ever played around with pulling out the the json key value pairs? Trying to figure out a way to iterate them into a standard map.
– HAL9000
Nov 19 at 22:51
Great! Sorry to say, I have very little experience with rapidjson. I settled with JSON for Modern C++ eventually but I don't know how to do that there either. I've only been doing very basic stuff with it.
– Ted Lyngmo
Nov 19 at 23:22
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
This gave me no error:
#include <iostream>
#include "rapidjson/document.h"
#include "rapidjson/error/en.h"
using namespace rapidjson;
int main() {
Document d;
std::string json = R"raw({"vehicle": {"brand": "zonda","color": "blue"},"username": {"brand": "doyota","color": "red"}})raw";
if (d.Parse(json.c_str()).HasParseError()) {
std::cout << "has errorn";
} else {
std::cout << "no errorn";
}
}
Tried C++11 - C++20 and it all seems fine. My guess is that you've got some non UTF8 character in the data.
Oh nice this worked for me too. I there a significance to the R and the prefixed and suffixed raw here? Thing is, I get the json string from a get call. {"vehicle": {"brand": "zonda","color": "blue"},"username": {"brand": "doyota","color": "red"}} I don't actually have this in the code. can I append R or "raw" like we do in java? I am kinda cpp newbie so out of my depth here.
– HAL9000
Nov 19 at 22:07
TheR"delimiter( raw_characters )delimiter"
only does you good with string literals so my guess is that you've got some non UTF8 character in the data but it's hard to say. But at least you know the parser is working. The string you get, can you save it in binary mode to a file and then perhaps validate it so that it is UTF8 encoded?
– Ted Lyngmo
Nov 19 at 22:18
That was it! I was being rather stupid and returning VERBOSE and HEADER in libcurl get call. With just the JSON string in the body it works correctly!
– HAL9000
Nov 19 at 22:50
Have you ever played around with pulling out the the json key value pairs? Trying to figure out a way to iterate them into a standard map.
– HAL9000
Nov 19 at 22:51
Great! Sorry to say, I have very little experience with rapidjson. I settled with JSON for Modern C++ eventually but I don't know how to do that there either. I've only been doing very basic stuff with it.
– Ted Lyngmo
Nov 19 at 23:22
add a comment |
up vote
1
down vote
accepted
This gave me no error:
#include <iostream>
#include "rapidjson/document.h"
#include "rapidjson/error/en.h"
using namespace rapidjson;
int main() {
Document d;
std::string json = R"raw({"vehicle": {"brand": "zonda","color": "blue"},"username": {"brand": "doyota","color": "red"}})raw";
if (d.Parse(json.c_str()).HasParseError()) {
std::cout << "has errorn";
} else {
std::cout << "no errorn";
}
}
Tried C++11 - C++20 and it all seems fine. My guess is that you've got some non UTF8 character in the data.
Oh nice this worked for me too. I there a significance to the R and the prefixed and suffixed raw here? Thing is, I get the json string from a get call. {"vehicle": {"brand": "zonda","color": "blue"},"username": {"brand": "doyota","color": "red"}} I don't actually have this in the code. can I append R or "raw" like we do in java? I am kinda cpp newbie so out of my depth here.
– HAL9000
Nov 19 at 22:07
TheR"delimiter( raw_characters )delimiter"
only does you good with string literals so my guess is that you've got some non UTF8 character in the data but it's hard to say. But at least you know the parser is working. The string you get, can you save it in binary mode to a file and then perhaps validate it so that it is UTF8 encoded?
– Ted Lyngmo
Nov 19 at 22:18
That was it! I was being rather stupid and returning VERBOSE and HEADER in libcurl get call. With just the JSON string in the body it works correctly!
– HAL9000
Nov 19 at 22:50
Have you ever played around with pulling out the the json key value pairs? Trying to figure out a way to iterate them into a standard map.
– HAL9000
Nov 19 at 22:51
Great! Sorry to say, I have very little experience with rapidjson. I settled with JSON for Modern C++ eventually but I don't know how to do that there either. I've only been doing very basic stuff with it.
– Ted Lyngmo
Nov 19 at 23:22
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
This gave me no error:
#include <iostream>
#include "rapidjson/document.h"
#include "rapidjson/error/en.h"
using namespace rapidjson;
int main() {
Document d;
std::string json = R"raw({"vehicle": {"brand": "zonda","color": "blue"},"username": {"brand": "doyota","color": "red"}})raw";
if (d.Parse(json.c_str()).HasParseError()) {
std::cout << "has errorn";
} else {
std::cout << "no errorn";
}
}
Tried C++11 - C++20 and it all seems fine. My guess is that you've got some non UTF8 character in the data.
This gave me no error:
#include <iostream>
#include "rapidjson/document.h"
#include "rapidjson/error/en.h"
using namespace rapidjson;
int main() {
Document d;
std::string json = R"raw({"vehicle": {"brand": "zonda","color": "blue"},"username": {"brand": "doyota","color": "red"}})raw";
if (d.Parse(json.c_str()).HasParseError()) {
std::cout << "has errorn";
} else {
std::cout << "no errorn";
}
}
Tried C++11 - C++20 and it all seems fine. My guess is that you've got some non UTF8 character in the data.
edited Nov 19 at 23:23
answered Nov 19 at 21:53
Ted Lyngmo
1,261214
1,261214
Oh nice this worked for me too. I there a significance to the R and the prefixed and suffixed raw here? Thing is, I get the json string from a get call. {"vehicle": {"brand": "zonda","color": "blue"},"username": {"brand": "doyota","color": "red"}} I don't actually have this in the code. can I append R or "raw" like we do in java? I am kinda cpp newbie so out of my depth here.
– HAL9000
Nov 19 at 22:07
TheR"delimiter( raw_characters )delimiter"
only does you good with string literals so my guess is that you've got some non UTF8 character in the data but it's hard to say. But at least you know the parser is working. The string you get, can you save it in binary mode to a file and then perhaps validate it so that it is UTF8 encoded?
– Ted Lyngmo
Nov 19 at 22:18
That was it! I was being rather stupid and returning VERBOSE and HEADER in libcurl get call. With just the JSON string in the body it works correctly!
– HAL9000
Nov 19 at 22:50
Have you ever played around with pulling out the the json key value pairs? Trying to figure out a way to iterate them into a standard map.
– HAL9000
Nov 19 at 22:51
Great! Sorry to say, I have very little experience with rapidjson. I settled with JSON for Modern C++ eventually but I don't know how to do that there either. I've only been doing very basic stuff with it.
– Ted Lyngmo
Nov 19 at 23:22
add a comment |
Oh nice this worked for me too. I there a significance to the R and the prefixed and suffixed raw here? Thing is, I get the json string from a get call. {"vehicle": {"brand": "zonda","color": "blue"},"username": {"brand": "doyota","color": "red"}} I don't actually have this in the code. can I append R or "raw" like we do in java? I am kinda cpp newbie so out of my depth here.
– HAL9000
Nov 19 at 22:07
TheR"delimiter( raw_characters )delimiter"
only does you good with string literals so my guess is that you've got some non UTF8 character in the data but it's hard to say. But at least you know the parser is working. The string you get, can you save it in binary mode to a file and then perhaps validate it so that it is UTF8 encoded?
– Ted Lyngmo
Nov 19 at 22:18
That was it! I was being rather stupid and returning VERBOSE and HEADER in libcurl get call. With just the JSON string in the body it works correctly!
– HAL9000
Nov 19 at 22:50
Have you ever played around with pulling out the the json key value pairs? Trying to figure out a way to iterate them into a standard map.
– HAL9000
Nov 19 at 22:51
Great! Sorry to say, I have very little experience with rapidjson. I settled with JSON for Modern C++ eventually but I don't know how to do that there either. I've only been doing very basic stuff with it.
– Ted Lyngmo
Nov 19 at 23:22
Oh nice this worked for me too. I there a significance to the R and the prefixed and suffixed raw here? Thing is, I get the json string from a get call. {"vehicle": {"brand": "zonda","color": "blue"},"username": {"brand": "doyota","color": "red"}} I don't actually have this in the code. can I append R or "raw" like we do in java? I am kinda cpp newbie so out of my depth here.
– HAL9000
Nov 19 at 22:07
Oh nice this worked for me too. I there a significance to the R and the prefixed and suffixed raw here? Thing is, I get the json string from a get call. {"vehicle": {"brand": "zonda","color": "blue"},"username": {"brand": "doyota","color": "red"}} I don't actually have this in the code. can I append R or "raw" like we do in java? I am kinda cpp newbie so out of my depth here.
– HAL9000
Nov 19 at 22:07
The
R"delimiter( raw_characters )delimiter"
only does you good with string literals so my guess is that you've got some non UTF8 character in the data but it's hard to say. But at least you know the parser is working. The string you get, can you save it in binary mode to a file and then perhaps validate it so that it is UTF8 encoded?– Ted Lyngmo
Nov 19 at 22:18
The
R"delimiter( raw_characters )delimiter"
only does you good with string literals so my guess is that you've got some non UTF8 character in the data but it's hard to say. But at least you know the parser is working. The string you get, can you save it in binary mode to a file and then perhaps validate it so that it is UTF8 encoded?– Ted Lyngmo
Nov 19 at 22:18
That was it! I was being rather stupid and returning VERBOSE and HEADER in libcurl get call. With just the JSON string in the body it works correctly!
– HAL9000
Nov 19 at 22:50
That was it! I was being rather stupid and returning VERBOSE and HEADER in libcurl get call. With just the JSON string in the body it works correctly!
– HAL9000
Nov 19 at 22:50
Have you ever played around with pulling out the the json key value pairs? Trying to figure out a way to iterate them into a standard map.
– HAL9000
Nov 19 at 22:51
Have you ever played around with pulling out the the json key value pairs? Trying to figure out a way to iterate them into a standard map.
– HAL9000
Nov 19 at 22:51
Great! Sorry to say, I have very little experience with rapidjson. I settled with JSON for Modern C++ eventually but I don't know how to do that there either. I've only been doing very basic stuff with it.
– Ted Lyngmo
Nov 19 at 23:22
Great! Sorry to say, I have very little experience with rapidjson. I settled with JSON for Modern C++ eventually but I don't know how to do that there either. I've only been doing very basic stuff with it.
– Ted Lyngmo
Nov 19 at 23:22
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382781%2fadd-values-to-map-using-rapidjson%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
RapidJSON allows you to check what the error is. Have you tried looking at the specific error code? The list is pretty extensive: rapidjson.org/md_doc_dom.html#ParseError
– ahota
Nov 19 at 21:35
As far as the map is concerned, I've been working on something similar recently. The closest I found to a true JSON (and Python dictionary) is an
std::unordered_map
with a key typestd::string
and a value type ofstd::variant<>
, where the variant could be int, float, string, vector, etc.std::variant
requires C++17, though.– ahota
Nov 19 at 21:39
@ahota the error says "invalid values". Although i suspect this could be because I am converting a standard string to const char*? Also i am restricted to cpp 11
– HAL9000
Nov 19 at 21:47