Problem
It seems by the context of your question that you want the "" removed so you can have a proper JSON format.
For the JSON parsing part, you should use a library. I'm going to post an example of it here, soon.
Solution
In order to do that, we are going to use the std::replace
function from the <algorithm>
library; although we can implement this on our own, it is better to use standard libraries since the creators of them worked hard on optimizing those function to the fullest capabilities. So let's take your code you gave us from the question and make it JSON-appropriate.
#include <algorithm>
#include <string>
#include <iostream>
using std::string;
using std::cout;
using std::endl;
void convert_char(string &s,char from_conv, char to_conv) {
std::replace( s.begin(), s.end(), from_conv, to_conv); // replace all 'x' to 'y'
}
int main()
{
string str = "{ \n \
\"key1\": [[\"1\", 0.4], [\"0\", 0.6]], \n \
\"key2\": true, \n \
\"key3\": 1, \n \
\"key4\": [{\"key41\": 1}, {\"key42\": [1,2,3]}] \n }";;
convert_char(str,'\"',(char)0);
cout << str << endl;
}
You can see here we have a function called convert_char
which converts a certain character to another. So basically as your question asked we removed the double quotation, and tada, it is formatted like JSON! Take a look here for the demo.
Solution to JSON Parser
Obviously, here you will use a library to do this for you. I'm going to introduce sciter
to you! Basically, with sciter
all you got to do is:
#include <algorithm>
#include <string>
#include <iostream>
#include <sciter>
using std::string;
using std::cout;
using std::endl;
int main()
{
string str = "{ \n \
\"key1\": [[\"1\", 0.4], [\"0\", 0.6]], \n \
\"key2\": true, \n \
\"key3\": 1, \n \
\"key4\": [{\"key41\": 1}, {\"key42\": [1,2,3]}] \n }";;
sciter::value str_conv = sciter::value::from_string( str, CVT_JSON_LITERAL );
cout << str_conv << endl;
}
Now according to this code, the JSON Formatted code is in str_conv
! References to guide you through this are below in the References section.
References:
sciter
cpprefrence std::replace
string::replace
cpprefrence std::refrence_if
Glossary
std::replace
:
Prototype:
template <class ForwardIterator, class T>
void replace (ForwardIterator first, ForwardIterator last,
const T& old_value, const T& new_value); //source cpprefrence
references: cpprefrence
<algorithm>
:
Many topics are inside the algorithm library. It's a library for, you guessed it, algorithms.
The header <algorithm>
defines a collection of functions especially designed to be used on ranges of elements.
A range is any sequence of objects that can be accessed through iterators or pointers, such as an array or an instance of some of the STL containers. Notice though, that algorithms
operate through iterators
directly on the values, not affecting in any way the structure of any possible container (it never affects the size or storage allocation of the container).
The algorithms library defines functions for a variety of purposes (e.g. searching, sorting, counting, manipulating) that operate on ranges of elements.
References:
cplusplus
cpprefrence