user2142786
user2142786

Reputation: 1482

Number Format exception in GSON while parsing json

I'm getting below exception,

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

Here is 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":""  
   }
}
]

and in my code we've two POJO classes and a Read Json business class

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

}

And a business logic class 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();
        }

    }

}

There are several question already asked related to this but those are for numeric or double value. In my json i have string+numeric.

Can anyone help on this.

Upvotes: 0

Views: 769

Answers (1)

vatsal mevada
vatsal mevada

Reputation: 5616

That is because ProductNameGlobalattribute is of type int in your Global class. In your JSON you are passing String value for that field "ProductNameGlobal":"Instrument1".

May be you need to change ProductNameGlobal type to String if your business logic allows that.

Upvotes: 1

Related Questions