1

It's a beginner question, how would I format the following json array of strings into std::string

[
  { 
    "x" : 12.1,
    "y" : 12.1,
    "z" : 12.1
  },
  { 
    "x" : 12.1,
    "y" : 12.1,
    "z" : 12.1
  },  
  { 
    "x" : 12.1,
    "y" : 12.1,
    "z" : 12.1
  },  
  { 
    "x" : 12.1,
    "y" : 12.1,
    "z" : 12.1
  }
]

Here is the json string

const std::string json =
            "[\n"
            "  {\n"
            "    \"x\" : 0,\n"
            "    \"y\" : 0,\n"
            "    \"z\" : 0\n"
            "  },\n"
            "  {\n"
            "    \"x\" : 640,\n"
            "    \"y\" : 0,\n"
            "    \"z\" : 0\n"
            "  },\n"
            "  {\n"
            "    \"x\" : 640,\n"
            "    \"y\" : 0,\n"
            "    \"z\" : 480\n"
            "  },\n"
            "  {\n"
            "    \"x\" : 0,\n"
            "    \"y\" : 0,\n"
            "    \"z\" : 480\n"
            "  }\n"
            "]\n";


        Json::Value coordinates;
        Json::Reader reader;

        reader.parse( json, coordinates );

So I'm trying to parse the above json array, to get a list of coordinates, but it can't be parsed correctly.

10
  • Json is a string. It's not code. "[{"x:12.1},{"x":12.1"}]" is a Json string. What do you mean? Are you trying to create a Commented Aug 30, 2017 at 8:58
  • my question how to write that json into std::string Commented Aug 30, 2017 at 8:59
  • Just like any other string. Json is a string, whose contents have a specific format. Nothing more. Commented Aug 30, 2017 at 9:00
  • what about the x, how would I make it work should I put +"\x"+ Commented Aug 30, 2017 at 9:00
  • 3
    Which is the error message ? Commented Aug 30, 2017 at 9:31

1 Answer 1

6

You might use raw string since C++11:

const std::string json = R"(
[
  { 
    "x" : 12.1,
    "y" : 12.1,
    "z" : 12.1
  },
  { 
    "x" : 12.1,
    "y" : 12.1,
    "z" : 12.1
  },  
  { 
    "x" : 12.1,
    "y" : 12.1,
    "z" : 12.1
  },  
  { 
    "x" : 12.1,
    "y" : 12.1,
    "z" : 12.1
  }
]
)";

before, you have to do some escaping as " -> \":

const std::string json = 
"[\n"
"  {\n"
"    \"x\" : 12.1,\n"
"    \"y\" : 12.1,\n"
"    \"z\" : 12.1\n"
"  },\n"
"  {\n"
"    \"x\" : 12.1,\n"
"    \"y\" : 12.1,\n"
"    \"z\" : 12.1\n"
"  },\n" 
"  {\n"
"    \"x\" : 12.1,\n"
"    \"y\" : 12.1,\n"
"    \"z\" : 12.1\n"
"  },\n"
"  {\n"
"    \"x\" : 12.1,\n"
"    \"y\" : 12.1,\n"
"    \"z\" : 12.1\n"
"  }\n"
"]\n";
Sign up to request clarification or add additional context in comments.

6 Comments

Also before he had to escape \n. For completeness: en.cppreference.com/w/cpp/language/string_literal see: prefix(optional) R "delimiter( raw_characters )delimiter" (6) (since C++11).
Can you write it without the Raw, just for a complete answer
There is no need to use newlines. Whitespace is ignored in Json. Instead of " one can also use ' and avoid escaping altogether
@andre post actual code that reproduces your problem and include the full error message. It may be that you used invalid tags, or that a required tag is missing. Or it could be a quirk of the parser library. What is Json::Reader ?
@andre not yet. What is Json::Reader ? What is the error message? C++ doesn't have any Json support and the string is OK as it is.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.