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

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

问题描述

我想使用json simple library读取这个带有java的 JSON 文件。

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

我的 JSON 文件如下所示:

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

这是java代码我写信读这个文件:

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

但我收到以下异常:


线程main中的异常java.lang.ClassCastException:
org.json.simple.JSONArray无法强制转换为org.json.simple.JSONObject
at javaapplication1.JavaApplication1.main(JavaApplication1.java:24)

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)

有人能说出来吗我,我做错了什么?整个文件是一个数组,文件的整个数组中有对象和另一个数组(汽车)。但我不知道如何将整个数组解析成java数组。我希望有人可以帮助我使用我的代码中缺少的代码行。

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.

谢谢

推荐答案


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

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

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

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+"");
    }
  }

有关参考,请参阅<上的示例1 a href =http://code.google.com/p/json-simple/wiki/DecodingExamples =noreferrer> json-simple decoding example 页。

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

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