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.

andre
  • 61
  • 2
  • 7
  • 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 – Panagiotis Kanavos Aug 30 '17 at 08:58
  • my question how to write that json into std::string – andre Aug 30 '17 at 08:59
  • Just like any other string. Json is a string, whose contents have a specific format. Nothing more. – Panagiotis Kanavos Aug 30 '17 at 09:00
  • what about the x, how would I make it work should I put +"\x"+ – andre Aug 30 '17 at 09:00
  • You are asking how to insert *quotes* and *double quotes* into a string. This has nothing to do with Json. The easiest solution is to use `'` instead of `"`. It's still valid JSON. Otherwise, you need to escape any special characters – Panagiotis Kanavos Aug 30 '17 at 09:02
  • @PanagiotisKanavos _“The easiest solution is to use `'` instead of `"`”._ Not in C++. https://stackoverflow.com/questions/7459939/what-do-single-quotes-do-in-c-when-used-on-multiple-characters – Melebius Aug 30 '17 at 09:15
  • @Melebius what you link to has nothing to do with string **contents**. You can have a string that contains single quotes – Panagiotis Kanavos Aug 30 '17 at 09:18
  • @PanagiotisKanavos So you meant inside the JSON? I understood you advised to put single quotes around it, like you could do in PHP, Python or shell, for example. – Melebius Aug 30 '17 at 09:19
  • 3
    Which is the error message ? – Jarod42 Aug 30 '17 at 09:31
  • @Melebius `You are asking how to insert quotes and double quotes into a string ... ` This is a question about Json, where it's very common to use single instead of double quotes – Panagiotis Kanavos Aug 30 '17 at 09:49

1 Answers1

5

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";
Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • Also before he had to escape `\n`. For completeness: http://en.cppreference.com/w/cpp/language/string_literal see: `prefix(optional) R "delimiter( raw_characters )delimiter" (6) (since C++11)`. – Andre Kampling Aug 30 '17 at 09:03
  • Can you write it without the Raw, just for a complete answer – andre Aug 30 '17 at 09:11
  • There is no need to use newlines. Whitespace is ignored in Json. Instead of `"` one can also use `'` and avoid escaping altogether – Panagiotis Kanavos Aug 30 '17 at 09:12
  • Unfortuantely, the Raw macro, doesn't work with Json reader Json::Value coordinates; Json::Reader reader; reader.parse( jsonstring, coordinates ); – andre Aug 30 '17 at 09:13
  • 3
    @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` ? – Panagiotis Kanavos Aug 30 '17 at 09:16
  • 1
    @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. – Panagiotis Kanavos Aug 30 '17 at 09:51