【问题标题】:Parsing jsot with eclipse: 2d array用eclipse解析jsot:二维数组
【发布时间】:2014-12-25 16:21:00
【问题描述】:

我的问题是解析二维数组并修复错误。下面是我的 jason 文件、java 代码和错误列表。

这是我的 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" } },

这是我的 java 代码,我认为问题在于定义第二个数组:

    try {

        FileReader reader = new FileReader(filePath);

        JSONParser jsonParser = new JSONParser();
        JSONArray jsonArray = (JSONArray) jsonParser.parse(reader);


        Iterator i = jsonArray.iterator();
        while (i.hasNext()){

        JSONObject object = (JSONObject) i.next();

        .
        .
        .


       JSONArray productArray = (JSONArray) jsonParser.parse("product");


        Iterator j = productArray.iterator();

        while (j.hasNext())
        {
            JSONObject product = (JSONObject) j.next();

             long productId = (Long) product.get("productId");
             System.out.println("The id is: " + productId);


        }`

错误列表:Unexpected character (p) at position 0. at org.json.simple.parser.Yylex.yylex(Yylex.java:610) at org.json.simple.parser.JSONParser.nextToken(JSONParser.java:269) at org.json.simple.parser.JSONParser.parse(JSONParser.java:118) at org.json.simple.parser.JSONParser.parse(JSONParser.java:81) at org.json.simple.parser.JSONParser.parse(JSONParser.java:75) at com.domain.project.SveUMain.main(SveUMain.java:66)

【问题讨论】:

    标签: java arrays json eclipse


    【解决方案1】:

    看来错误来自这一行:

    JSONArray productArray = (JSONArray) jsonParser.parse("product");
    ...
    Unexpected character (p) at position 0.
    

    那行代码将尝试解析字符串“product”,就好像它是一个 JSON 字符串一样。当然,这不是,所以解析器会放弃抱怨第一个字符。

    如果您尝试访问每个 JSON 对象的“产品”字段,您可以这样做:

    Iterator i = jsonArray.iterator();
    while (i.hasNext()){
        JSONObject object = (JSONObject) i.next();
        JSONObject productObj = (JSONObject) object.get("product");
    

    JSON.simple 似乎没有一个函数可以通过一次调用从数组中的每个对象返回产品对象。

    【讨论】:

      【解决方案2】:

      这个 json 文件有语法错误。

      当您尝试在在线编辑器中验证文件时,它应该在第 1 行给您一个语法错误。

      一个 json 文件以一个对象“{”开始,然后你可以在其中使用你的数组。

      最后一个“,”也是错误的,因为后面没有元素。 (并且您需要关闭数组“]”和对象“}”。

      这里有两个例子,它们可能是你正确的 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"
        }
      }
      

      这就是第二种解决方案:

      {
        "array": [
          {
            "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"
            }
          }
        ]
      }
      

      【讨论】:

      • 后面的元素只是文件的一部分。
      猜你喜欢
      • 1970-01-01
      • 2020-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多