解析json时GSON中的数字格式异常

时间:2017-09-10 10:52:00

标签: java json gson

我在异常之下,

Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: For input string: "Instrument1"

这是JSON

[{  
   "_id":"INS_123",
   "global_us":{  
      "ProductID":"INS_123",
      "ProductNameGlobal":"Instrument1",
      "Brand Name":"Brand",
      "ProductType":"Instrument",
      "Status":"Avail",
      "Channel":"Channel",
      "InstrumemtCode":"Instrument1",
      "LifeCycleStatus":""
   },
   "en_us":{  
      "ProductID":"INS_123",
      "ProductNameGlobal":"Instrument1",
      "Brand Name":"Brand",
      "ProductType":"Instrument",
      "Status":"Avail",
      "Channel":"Channel",
      "InstrumemtCode":"Instrument1",
      "LifeCycleStatus":""   
   }
},
{  
   "_id":"INS_124",
   "global_us":{  
      "ProductID":"INS_124",
      "ProductNameGlobal":"Instrument2",
      "Brand Name":"Brand",
      "ProductType":"Instrument",
      "Status":"Avail",
      "Channel":"Channel",
      "InstrumemtCode":"Instrument2",
      "LifeCycleStatus":""
   },
   "en_us":{  
      "ProductID":"INS_124",
      "ProductNameGlobal":"Instrument2",
      "Brand Name":"Brand",
      "ProductType":"Instrument",
      "Status":"Avail",
      "Channel":"Channel",
      "InstrumemtCode":"Instrument2",
      "LifeCycleStatus":""  
   }
}
]

在我的代码中,我们有两个POJO类和一个Read Json业务类

public class Product {

    private Global global_us;

    public Global getGlobal_us() {
        return global_us;
    }

    public void setGlobal_us(Global global_us) {
        this.global_us = global_us;
    }

    private String ProductID;
    private int ProductGlobalName;

    public String getProductID() {
        return ProductID;
    }

    public void setProductID(String productID) {
        ProductID = productID;
    }

    public int getProductGlobalName() {
        return ProductGlobalName;
    }

    public void setProductGlobalName(int productGlobalName) {
        ProductGlobalName = productGlobalName;
    }

}

Global.java

public class Global {

    private String ProductID;
    private int ProductNameGlobal;

    public String getProductID() {
        return ProductID;
    }

    public void setProductID(String productID) {
        ProductID = productID;
    }

    public int getProductNameGlobal() {
        return ProductNameGlobal;
    }

    public void setProductNameGlobal(int productNameGlobal) {
        ProductNameGlobal = productNameGlobal;
    }

}

一个业务逻辑类ReadJson.java

package com.test.practice.gson;

import com.google.gson.Gson;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class ReadJson {

    public static void main(String[] args) {

        Gson gson = new Gson();

        try (Reader reader = new FileReader("D:\\json\\productlist.json")) {

            // Convert JSON to Java Object
            Product[] productList = gson.fromJson(reader, Product[].class);
            for (Product product : productList) {
                System.out.println(product.getGlobal_us().getProductNameGlobal());
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

有几个问题已经被问及与此相关,但这些是数字或双值。在我的json中我有字符串+数字。

任何人都可以帮忙解决这个问题。

1 个答案:

答案 0 :(得分:1)

这是因为ProductNameGlobal类中的Global属性类型为int。在您的JSON中,您要传递该字段"ProductNameGlobal":"Instrument1"的字符串值。

如果您的业务逻辑允许,可能需要将ProductNameGlobal类型更改为String