如何使用简单的 JSON 库将 json 文件读入 java [英] How to read json file into java with simple JSON library

查看:35
本文介绍了如何使用简单的 JSON 库将 json 文件读入 java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 json 简单库用 java 读取这个 JSON 文件.

我的 JSON 文件如下所示:

<预><代码>[{"name":"约翰","城市":"柏林",汽车":["奥迪",宝马"],"工作":"老师"},{"name":"标记","城市":"奥斯陆",汽车":[大众",《丰田》],"工作":"医生"}]

这是我为了读取这个文件而写的java代码:

package javaapplication1;导入 java.io.FileNotFoundException;导入 java.io.FileReader;导入 java.io.IOException;导入 java.util.Iterator;导入 org.json.simple.JSONArray;导入 org.json.simple.JSONObject;导入 org.json.simple.parser.JSONParser;导入 org.json.simple.parser.ParseException;公共类 JavaApplication1 {公共静态无效主(字符串 [] args){JSONParser 解析器 = 新的 JSONParser();尝试 {Object obj = parser.parse(new FileReader("c:\file.json"));JSONObject jsonObject = (JSONObject) obj;String name = (String) jsonObject.get("name");System.out.println(name);String city = (String) jsonObject.get("city");System.out.println(city);String job = (String) jsonObject.get("job");System.out.println(job);//循环数组JSONArray 汽车 = (JSONArray) jsonObject.get("cars");迭代器<字符串>迭代器 = 汽车.迭代器();而(迭代器.hasNext()){System.out.println(iterator.next());}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} catch (ParseException e) {e.printStackTrace();}}}

但我得到以下异常:

<块引用>

线程main"中的异常java.lang.ClassCastException:org.json.simple.JSONArray 不能转换为 org.json.simple.JSONObject在 javaapplication1.JavaApplication1.main(JavaApplication1.java:24)

谁能告诉我我做错了什么?整个文件是一个数组,文件的整个数组中有对象和另一个数组(汽车).但我不知道如何将整个数组解析为 java 数组.我希望有人可以帮助我解决我的代码中缺少的代码行.

谢谢

解决方案

整个文件是一个数组,文件的整个数组中有对象和其他数组(例如汽车).

如您所说,JSON blob 的最外层是一个数组.因此,您的解析器将返回一个 JSONArray.然后您可以从数组中获取 JSONObjects ...

 JSONArray a = (JSONArray) parser.parse(new FileReader("c:\exer4-courses.json"));对于(对象 o:a){JSONObject 人 = (JSONObject) o;String name = (String) person.get("name");System.out.println(name);String city = (String) person.get("city");System.out.println(city);String job = (String) person.get("job");System.out.println(job);JSONArray 汽车 = (JSONArray) person.get("cars");对于(对象 c:汽车){System.out.println(c+"");}}

有关参考,请参阅json-简单解码示例<中的示例 1"/a> 页.

I want to read this JSON file with java using json simple library.

My JSON file looks like this:

[  
    {  
        "name":"John",
        "city":"Berlin",
        "cars":[  
            "audi",
            "bmw"
        ],
        "job":"Teacher"
    },
    {  
        "name":"Mark",
        "city":"Oslo",
        "cars":[  
            "VW",
            "Toyata"
        ],
        "job":"Doctor"
    }
]

This is the java code I wrote to read this file:

package javaapplication1;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class JavaApplication1 {
    public static void main(String[] args) {

        JSONParser parser = new JSONParser();

        try {     
            Object obj = parser.parse(new FileReader("c:\file.json"));

            JSONObject jsonObject =  (JSONObject) obj;

            String name = (String) jsonObject.get("name");
            System.out.println(name);

            String city = (String) jsonObject.get("city");
            System.out.println(city);

            String job = (String) jsonObject.get("job");
            System.out.println(job);

            // loop array
            JSONArray cars = (JSONArray) jsonObject.get("cars");
            Iterator<String> iterator = cars.iterator();
            while (iterator.hasNext()) {
             System.out.println(iterator.next());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

But I get the following exception:

Exception in thread "main" java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject at javaapplication1.JavaApplication1.main(JavaApplication1.java:24)

Can somebody tell me what I am doing wrong? The whole file is a array and there are objects and another array (cars) in the whole array of the file. But i dont know how I can parse the whole array into a java array. I hope somebody can help me with a code line which I am missing in my code.

Thanks

解决方案

The whole file is an array and there are objects and other arrays (e.g. cars) in the whole array of the file.

As you say, the outermost layer of your JSON blob is an array. Therefore, your parser will return a JSONArray. You can then get JSONObjects from the array ...

  JSONArray a = (JSONArray) parser.parse(new FileReader("c:\exer4-courses.json"));

  for (Object o : a)
  {
    JSONObject person = (JSONObject) o;

    String name = (String) person.get("name");
    System.out.println(name);

    String city = (String) person.get("city");
    System.out.println(city);

    String job = (String) person.get("job");
    System.out.println(job);

    JSONArray cars = (JSONArray) person.get("cars");

    for (Object c : cars)
    {
      System.out.println(c+"");
    }
  }

For reference, see "Example 1" on the json-simple decoding example page.

这篇关于如何使用简单的 JSON 库将 json 文件读入 java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆