java - 使用 Eclipse 解析 JSON

标签 java json eclipse

我是 Java 的新手,我有这个可能很简单的教师项目。我必须用 Eclipse 解析 json,所以我开始了,但没有成功。当我在 json 中有多个对象时,我不知道如何开始。

我是这样开始的:

public static void main(String[] args) {

         FileReader reader = new FileReader(filePath);


            JSONParser jsonParser = new JSONParser();
            JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
            JSONArray objectArray = jsonObject.getJSONArray("product");


            //JSONObject site= jsonSites.getJSONObject(1);

            long elementaryProductId =  (long) jsonObject[0].get("elementaryProductId");
            System.out.println("The id is: " + elementaryProductId);'

这是我的 json 文件:

[{  
    "elementaryProductId":1,
    "bonusMalus":30,
    "deductible":500,
    "comprehensive":1,
    "partial":0,
    "legacyPremium":130,
    "product":{  
        "productId":2,
        "garage":"true",
        "constructionYear":1990,
        "region":"East",
        "dateOfBirthYoungest":"1983-06-22",
        "objectValue":25000,
        "type":"Car",
        "insuredObject":{  
            "name":"Car",
            "ownersName":"Jovana",
            "mileage":300000,
            "engineCapacity":120
        },
        "salesProduct":{  
            "salesProductId":3,
            "currency":"EUR",
            "contractStart":"2011-01-01",
            "contractEnd":"2012-01-01"
        },
        "productType":"Car"
    }
},
{  
    "elementaryProductId":1,
    "bonusMalus":5,
    "deductible":100,
    "comprehensive":1,
    "partial":0,
    "legacyPremium":75.38,
    "product":{  
        "productId":2,
        "garage":"true",
        "constructionYear":2005,
        "region":"East",
        "dateOfBirthYoungest":"1999-06-22",
        "objectValue":30000,
        "type":"Car",
        "insuredObject":{  
            "name":"Car",
            "ownersName":"Jelena",
            "mileage":300000,
            "engineCapacity":210
        },
        "salesProduct":{  
            "salesProductId":3,
            "currency":"EUR",
            "contractStart":"2013-01-01",
            "contractEnd":"2014-01-01"
        },
        "productType":"Car"
    }
}]

最佳答案

我让它与以下项目一起工作:

public static void main(String[] args) throws IOException, ParseException{
    FileReader reader = new FileReader(new File("filename.json"));


    JSONParser jsonParser = new JSONParser();
    JSONArray jsonArray = (JSONArray) jsonParser.parse(reader);
    JSONObject object = (JSONObject) jsonArray.get(0);
    long elementaryProductId = (Long) object.get("elementaryProductId");



    System.out.println("The id is: " + elementaryProductId);
}

上面的解释:

您知道最外层的元素是一个数组,因此直接解析为 JSONArray。接下来,您要提取该数组的第一个元素,即 JSONObject(位于大括号中)。之后代码应该是相当 self 解释的:)

关于java - 使用 Eclipse 解析 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27647527/

相关文章:

java - 将 net.sf.ehcache.CacheManger 转换为 org.springframework.cache.CacheManager?

java - 如何使用 GSON 库将 json 文件读入 java

java - 是否有 java.decompiler.free.fr 的替代品?

ios - 如何使用 Objective-C 初始化嵌套的 Node.js 模式

python - 导入同名包时在 Eclipse 中导入错误

java - 与另一个进程 Java 交互

python - Python 中的 JSON 提取。

javascript - 如何解析 JSON 对象并将值作为字符串显示在屏幕上?

java - 小部件的 Eclipse e4 图像图标以及如何让它们方便使用

Eclipse - 找不到父 pom,但 Maven 构建实际上可以从 Eclipse 本身运行