SachinKS
SachinKS
2013-08-02 03:47
采纳率: 85.7%
浏览 6.7k

解析包含null的原始json数组

Json数组:

{"userName":null," msgArr":[null],"numrow":0} //String result

Gson:

new Gson().fromJson(result, MyClass.class);

MyClass:

public String userName;
public int[] msgArr;
...

错误stack:

at java.lang.IllegalArgumentException: Primitive array can't take null values.
at java.lang.reflect.Array.set(Array.java:406)
at com.google.gson.internal.bind.ArrayTypeAdapter.read(ArrayTypeAdapter.java:78)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
at com.google.gson.Gson.fromJson(Gson.java:803)
at com.google.gson.Gson.fromJson(Gson.java:768)
at com.google.gson.Gson.fromJson(Gson.java:717)
at com.google.gson.Gson.fromJson(Gson.java:689)
at com.test.dao.RequestBase.onPostExecute(RequestBase.java:79)

如果不能修改json字符串 "msgArr=[null]",怎么解除这个异常?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • wurenbi45
    wurenbi45 2013-08-02 06:15
    已采纳

    json里得msgArr的值去掉{},直接写成null试试

    点赞 评论
  • hu00848
    Nolan_hu 2013-08-29 07:28

    GSON代码调整为下面的试试:

    new GsonBuilder()
    .seralizeNulls()//处理Null
    .create()
    .fromJson(result, MyClass.class);

    点赞 评论

相关推荐