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?










share|improve this question






















  • 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










  • @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

















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?










share|improve this question






















  • 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










  • @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















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?










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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




















  • 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










  • @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














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.






share|improve this answer























  • 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












  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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.






share|improve this answer























  • 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












  • 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















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.






share|improve this answer























  • 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












  • 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













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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








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










  • 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










  • 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










  • 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










  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python