-2

I'm trying to pass in a JSON file from within an app to populate some views and perform some functions within the app but I keep getting this error.

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $

I've gone through most of the previous answers on a similar subject, but none of the solutions are working for me.

This is me passing in my json file.

ArrayList<ControlProperty> controlProperty = new Gson().fromJson("json_file.json", new TypeToken<List<ControlProperty>>() {}.getType());

This is my model.

public class ControlProperty {
    private String type;
    private String label;
    private String name;
    private String subtype;
    private String dependency;
    private String description;
    private String placeholder;
    private String dependencyType;
    private String selectDependencyOption;
    private int minlength;
    private int maxlength;
    private long maxSize;
    private String pageType;
    private String expectedFormat;
    private String maxDate;
    private String minDate;
}

and then the json structure

[
  {
    "type": "Head",
    "label": "All fields are necessary.",
    "subtype": "h1",
    "dependency": "",
    "controlId": "frmb-1557846206638-fld-1",
    "currentDate": false,
    "pageType": "new-page",
    "headerSize": "s",
    "isFieldDependent": false
  },

  {
    "required": true,
    "name": "text-1550823191761",
    "type": "text_input",
    "label": "Surname",
    "minlength": "1",
    "maxlength": "50",
    "subtype": "text_input",
    "dependency": "",
    "controlId": "frmb-1557846206638-fld-2",
    "currentDate": false,
    "pushFieldLink": "surname",
    "isFieldDependent": false,
  },
  {
    "name": "text-1550823211048",
    "type": "text_input",
    "label": "Middle Name",
    "minlength": "1",
    "maxlength": "50",
    "subtype": "text",
    "dependency": "",
    "controlId": "frmb-1557846206638-fld-3",
    "currentDate": false,
    "pushFieldLink": "middle_name",
    "isFieldDependent": false,
  }
]
DanIke
  • 51
  • 10

2 Answers2

0

First problem: the following fields are defined as int in your data model but your JSON shows it as Strings: minlength and maxlength.

As for the actual Gson conversion:

  1. I'm assuming that you are replacing json_file.json with the actual JSON file content - the name of the file won't do.
  2. For the ArrayList itself, try this:
ArrayList<ControlProperty> items = new ArrayList<>(Arrays.asList(new Gson().fromJson(jsonString, ControlProperty[].class)));

This will convert Json string to ControlProperty class Array (ControlProperty[]) - and then you just add the array into the ArrayList.

ᴛʜᴇᴘᴀᴛᴇʟ
  • 4,466
  • 5
  • 39
  • 73
  • This was really helpful, but even after changing the fileds model and cinverting this way, the same error still occurs ``` ArrayList items = new ArrayList<>(Arrays.asList(new Gson().fromJson(jsonString, ControlProperty[].class))); ``` – DanIke Aug 27 '21 at 02:33
0

Along with what others have suggested on changing the types of properties in the class ControlProperty

            InputStream is = loader.getResourceAsStream("input.json");

            Reader reader = new InputStreamReader(is);

            Type listType = new TypeToken<List<ControlProperty>>() {}.getType();
            List<ControlProperty> posts = new Gson().fromJson(reader, listType)
vrunnn
  • 96
  • 6