0

I want to read Json file. My Json File has Content as follow

[
  {
    "arguments" : [
      {
        "IsEnabled" : "false",
        "class" : "UITextField",
        "width" : 238,
        "parent" : {
          "class" : "UIView",
          "height" : 101,
          "Y" : 192,
          "width" : 280,
          "X" : 20
        },
        "name" : "Enter UserName",
        "X" : 40,
        "isRightOf" : "NA",
        "Recording Device" : "NA",
        "Y" : 0
      },
      {
        "data" : "Enter UserName",
        "type" : "string"
      }
    ],

  }
]

I also tried GSON library to read JSON file.but fails . Please help

Ruby
  • 103
  • 1
  • 2
  • 14

2 Answers2

1

The JSON you provided is invalid (there is an invalid comma)

[
  {
    "arguments" : [
      {
        "IsEnabled" : "false",
        "class" : "UITextField",
        "width" : 238,
        "parent" : {
          "class" : "UIView",
          "height" : 101,
          "Y" : 192,
          "width" : 280,
          "X" : 20
        },
        "name" : "Enter UserName",
        "X" : 40,
        "isRightOf" : "NA",
        "Recording Device" : "NA",
        "Y" : 0
      },
      {
        "data" : "Enter UserName",
        "type" : "string"
      }
    ], <-- this comma makes the json invalid

  }
]
jHilscher
  • 1,810
  • 2
  • 25
  • 29
0

Your input json was wrong there is a comma missing as suggested.

Json objects are very hard to parse but if you once get the concept of how to parse the json data it is really easy.

You need to see if the property you are trying to access is a json array or an object. This is the basic rule if you are a beginner.

Here is the code::

OUTOUT IS::

arguments>>>>>>>>> [{"parent":{"width":280,"X":20,"Y":192,"class":"UIView","height":101},"Recording Device":"NA","IsEnabled":"false","width":238,"name":"Enter UserName","X":40,"isRightOf":"NA","Y":0,"class":"UITextField"},{"data":"Enter UserName","type":"string"}]
{"parent":{"width":280,"X":20,"Y":192,"class":"UIView","height":101},"Recording Device":"NA","IsEnabled":"false","width":238,"name":"Enter UserName","X":40,"isRightOf":"NA","Y":0,"class":"UITextField"}
{"data":"Enter UserName","type":"string"}

So, here in the code you can see that I have taken json array sometimes and object sometime, you need to differentiate between them.

import java.io.FileReader;
import java.io.IOException;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;


public class test {

    public static void main(String[] args) throws IOException, InterruptedException {
        JSONParser parser = new JSONParser();
        try {
            Object obj = parser.parse(new FileReader("test.json"));
            JSONArray jsonObject = (JSONArray) obj;
            JSONObject arr = (JSONObject) jsonObject.get(0);
            JSONArray arguments = (JSONArray) arr.get("arguments");
            System.out.println("arguments>>>>>>>>> "+arguments);
            for(int i = 0 ; i< arguments.size() ;i++){
                JSONObject object = (JSONObject) arguments.get(i);
                System.out.println(object);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Abhishek Honey
  • 645
  • 4
  • 13
  • thanks ,but this is not working.Actually Json is very big cannot able to edit the post or paste the whole file. – Ruby Sep 21 '17 at 04:03
  • Which properties of the json file you want to read? And where are you exactly getting the errors? – Abhishek Honey Sep 21 '17 at 05:43
  • i have a file having .json extension including data as follow [ { "arguments" : [{ "parent" : { }, }, { } ],"action" : "NA", "popupName" : "NA" } ] [ { "arguments" : [ { "parent" : { }, }, { } ], "action" : "NA", "popupName" : "NA" } ] error i ot while reading unexpected left square . – Ruby Sep 21 '17 at 06:12
  • This is not a valid json, you need to first validate the json file. Validate it first and give the correct json input then only I can help you. You can validate your json here https://jsonformatter.curiousconcept.com – Abhishek Honey Sep 21 '17 at 06:21