2

I am having some trouble building a class which will parse out gson as I expect it to.

I created a class.

public class JsonObjectBreakDown {
    public String type; 
    public List<String> coordinates = new ArrayList<String>();
}

And called

JsonObjectBreakDown p = gson.fromJson(withDup, JsonObjectBreakDown.class);

Below is my json

  {
   "type":"Polygon",
   "coordinates":[
      [
         [
            -66.9,
            18.05
         ],
         [
            -66.9,
            18.05
         ],
         [
            -66.9,
            18.06
         ],
         [
            -66.9,
            18.05
         ]
      ]
   ]
}

I have used gson before successfully, but never with an array like this. Should I not be using the List/ArrayList?

I get the error message;

Exception in thread "main" com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 31

OpenCSV code

CSVReader reader = new CSVReader(new FileReader("c:\\Json.csv"));
String tmp = reader.readNext();
CustomObject tmpObj = CustomObject(tmp[0], tmp[1],......);
1

1 Answer 1

4

The problem here is that you have an array of arrays of arrays of floating point numbers in your JSON. Your class should be

public class JsonObjectBreakDown {
    public String type; 
    public List<List<float[]>> coordinates = new ArrayList<>();
}

Parsing with the above and trying

System.out.println(p.coordinates.size());
System.out.println(p.coordinates.get(0).size());
System.out.println(Arrays.toString(p.coordinates.get(0).get(0)));

yields

1
2
[-66.9, 18.05]
Sign up to request clarification or add additional context in comments.

11 Comments

@user2524908 Yes, but if you are using Java 7, you don't need to put the argument in the constructor call. Just new ArrayList<>();
hm, I am getting 'Unterminated object at line 1 column 31.' I assume that means there is a problem with my json. I validated it correct format with some online json validator.
@user2524908 Can you edit your question with the actual json String you are using? I ran the code and it works.
I updated the json. Unfortunately it looks like my openvsc parser is stripping out one of the closing " after Polygon when I output it in eclipse
@user2524908 Before you go and fix how the CSV parsing happens, try to write the String out yourself with proper escaping and checking if that works for you.
|

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.