0

I got a complex JSON string like this.

  "rectangle": 
    "{\n  \
    "minX\": 0.0,\n  \
    "minY\": 0.0,\n  \
    "maxX\": 2460.0,\n  \
    "maxY\": 3008.0\n}",
  "graphSpace": 
    "[
       [{\
        "rectangle\":
            \"{\\n  \\\
            "minX\\\": 0.0,\\n  \\\
            "minY\\\": 0.0,\\n  \\\
            "maxX\\\": 0.0,\\n  \\\
            "maxY\\\": 0.0\\n}\",\

This is not complete. But how to read this?

1 Answers1

0

Your "json string" is not a string. It is json data (as seen in a json file).

All the values of this json data are "stringified json". The second part of the json data, under graphSpace is double stringified. First lets understand what you have and then we'll read it into Java.

When formatted it is clearer what we have:

"rectangle": 
    "{\n  
      \"minX\": 0.0,\n  
      \"minY\": 0.0,\n  
      \"maxX\": 2460.0,\n  
      \"maxY\": 3008.0\n}",
"graphSpace": 
  "[[{
     \"rectangle\":
        \"{\\n  
        \\\"minX\\\": 0.0,\\n  
        \\\"minY\\\": 0.0,\\n  
        \\\"maxX\\\": 0.0,\\n  
        \\\"maxY\\\": 0.0\\n}\",
 \ 

The last comma and backslash are part of some continued JSON which you did not show. the json string value of graphSpace is incomplete in your example and there is no closing double quote. (The \" on the last line of code is just part of the string and is "escaped" with a backslash.

\ is the way to write a single backslash \ inside a string. \" is the way to write a double quote " inside a string.

string s1 = "the following double quote \" is not the end of the string.";
string s2 = "while the double quote after the next period does close the string."; 

So the rectangle holds a json string, which itself can be parsed into a js object.

The graphSpace also holds a json string, but this json string when parsed into a js object will hold an array of array of rectangle objects (with a single rectangle object in it), and this rectangle object AFTER PARSED holds a json string.

I'll say it again. After parsing the graphSpace json, this rectangle object will hold a single json string, without the extra double quotes, so you will have a javascript object with the following

myobj.graphSpace = 
  [[{
     rectangle:
        "{\n  
          \"minX\": 0.0,\n  
          \"minY\": 0.0,\n  
          \"maxX\": 0.0,\n  
          \"maxY\": 0.0\n}\"
// and I presume the rest of the missing code
         }", // etc. etc. 

Somewhere in your code that json string may be parsed too. So you'll write: (using google GSon for example)

Gson gson = new Gson();
JsonReader reader = new JsonReader(new FileReader(filename));
Rectangle rect1 = gson.fromJson(reader, RECTANGLE_TYPE); // see link below for further details
GraphSpace graphSpace = gson.fromJson(reader, GRAPHSPACE_TYPE);

// at this stage you have parsed in all the json file. 
// but you may wish to continue and parse the stringified value of the graphSpace.rectangle so: 
string rectString = graphSpace[0][0].rectangle;
Rectangle graphRect = gson.fromJson(rectString, RECTANGLE_TYPE);
// or use a `for` loop to loop through the rectangle strings in graphSpace...

In Java there is a built-in way to read JSON and there are many good libraries to do so.

See

pashute
  • 3,965
  • 3
  • 38
  • 65