我有这个科特林课
class CategoryJsonData(val category: List<Category>)
data class Category(val _id:Double, val category_name:String, val sub_category_name:String, val price:Int)
在这一行,我得到一个错误
val jsonData = gson.fromJson(body, CategoryJsonData::class.java)
当我解析此JSON
{ "_id": "5b80393f7fca491488980fe1", "category_name": "electrical", "sub_category_name": "switches meters and fuses", "price": 300 }
java.lang.NumberFormatException:无效的双精度:“ 5b80393f7fca491488980fe1”
5b80393f7fca491488980fe1(_id)
是由猫鼬使用nodejs生成的。
尝试点击API时显示此错误。
09-18 23:26:47.026 18264-18542/com.hiretheskill.skill E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: com.hiretheskill.skill, PID: 18264
java.lang.NumberFormatException: Invalid double: "5b80393f7fca491488980fe1"
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.initialParse(StringToReal.java:164)
at java.lang.StringToReal.parseDouble(StringToReal.java:282)
at java.lang.Double.parseDouble(Double.java:301)
at com.google.gson.stream.JsonReader.nextDouble(JsonReader.java:909)
at com.google.gson.Gson$2.read(Gson.java:284)
at com.google.gson.Gson$2.read(Gson.java:278)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
at com.google.gson.Gson.fromJson(Gson.java:887)
at com.google.gson.Gson.fromJson(Gson.java:852)
at com.google.gson.Gson.fromJson(Gson.java:801)
at com.google.gson.Gson.fromJson(Gson.java:773)
at com.hiretheskill.skill.SubCategories$fetchJsonData$client$1.onResponse(SubCategories.kt:45)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:153)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
答案 0 :(得分:3)
In your JSON, this is a String "_id": "5b80393f7fca491488980fe1"
In your class, you defined it as a Double val _id:Double
You need to change that to be a String since you have non-numerical characters in the JSON value