java - Number Format exception in GSON while parsing json -
i'm getting below exception,
exception in thread "main" com.google.gson.jsonsyntaxexception: java.lang.numberformatexception: input string: "instrument1"
here 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 code we've 2 pojo classes , 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 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 java object product[] productlist = gson.fromjson(reader, product[].class); (product product : productlist) { system.out.println(product.getglobal_us().getproductnameglobal()); } } catch (ioexception e) { e.printstacktrace(); } } }
there several question asked related numeric or double value. in json have string+numeric.
can on this.
that because productnameglobal
attribute of type int in global
class. in json passing string value field "productnameglobal":"instrument1"
.
may need change productnameglobal
type string
if business logic allows that.
Comments
Post a Comment