java 如何在java中读取json文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46322131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 09:09:36  来源:igfitidea点击:

How to read json file in java

javajson

提问by Ruby

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

我想阅读 Json 文件。我的 Json 文件内容如下

[
  {
    "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

我也尝试过 GSON 库来读取 JSON 文件。但失败了。请帮忙

回答by jHilscher

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

您提供的 JSON 无效(有一个无效的逗号)

[
  {
    "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

  }
]

回答by ABHISHEK HONEY

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

您的输入 json 错误,建议缺少逗号。

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.

Json 对象很难解析,但是如果您曾经了解如何解析 json 数据的概念,那真的很容易。

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.

您需要查看您尝试访问的属性是 json 数组还是对象。如果您是初学者,这是基本规则。

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.

所以,在代码中你可以看到我有时使用json数组,有时使用对象,你需要区分它们。

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();
        }
    }
}