0

I'm kinda getting frustrated after searching and trying around everything my mind came up with...

I try to parse a JSON file into a list or array of MyObject. I found this post Link and played around with the code. But now I always get the same Exception, whatever I do/change in the JSON file.

Exception I get:

SyntaxError: JSON.parse: unexpected non-digit at line 1 column 2 of the JSON data

What I tried:

I reduced the file to just 2 objects not containing some special stuff to make sure it works. It doesnt..

  • Validating the json file with an online tool
  • All different types of importing the JSON as object list/array from 1
  • playing around with the JSON file

Heres the current code for importing

List<MyClass> myObjects = Arrays.asList(mapper.readValue(content, MyClass[].class));

JSON

[
    {
        "name":"1000.1000",
        "maskId":"1000",
        "fieldId":"1000",
        "i18nKey":"debugLabel_1",
        "label":"Logo",
        "tooltip":" ---"
    },
    {
        "name":"1000.1000",
        "maskId":"1000",
        "fieldId":"1000",
        "i18nKey":"debugLabel_1",
        "label":"Logo",
        "tooltip":" ---"
    }
]

MyClass

 public class MyClass{

        String name;
        String maskId;
        String fieldId;
        String i18nKey;
        String label;
        String tooltip;

        public MyClass(String name, String maskId, String fieldId, String i18nKey, String tooltip, String label) {
            this.name = name;
            this.maskId = maskId;
            this.fieldId = fieldId;
            this.i18nKey = i18nKey;
            this.tooltip = tooltip;
            this.label = label;
        }

        // Getter + Setter
    }

Thanks for advices in advance.

3 Answers3

0

It seems that your class expects to receive a number for it's property and sees string.

This should work:

[
    {
        "name":"1.1",
        "maskId":1,
        "fieldId":"1",
        "i18nKey":"test1",
        "label":"Test 1",
        "tooltip":"---"
    },
    {
        "name":"1.2",
        "maskId":1,
        "fieldId":"2",
        "i18nKey":"test2",
        "label":"Test 2",
        "tooltip":"---"
    }
]
0
Gson gson = new GsonBuilder().create();
final String jsonString = gson.toJson(mapper.readValue(content, MyClass[].class);

try this, i think it will work

Fatima Naik
  • 304
  • 3
  • 11
0

I solved it... but I still cant tell what the problem was.

I recreated all the files and Classes and nowit works... kinda strange

But thanks for the help!