Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 解析json时GSON中出现数字格式异常_Java_Json_Gson - Fatal编程技术网

Java 解析json时GSON中出现数字格式异常

Java 解析json时GSON中出现数字格式异常,java,json,gson,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":"In

我的情况越来越糟了

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类和一个ReadJSON业务类

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中,我有字符串+数字


有人能帮上忙吗。

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


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

顺便说一句,使用开头属性名称加大写字母不是一个好主意。同意。。。这是错的