永发信息网

怎么返回得到的gson.fromjson

答案:1  悬赏:60  手机版
解决时间 2021-03-20 08:23
  • 提问者网友:姑娘长的好罪过
  • 2021-03-19 13:28
怎么返回得到的gson.fromjson
最佳答案
  • 五星知识达人网友:野味小生
  • 2021-03-19 13:45
import com.google.gson.Gson;

public class TestJson
{
private int age;
private String name;

public String show()
{
return new String("name:" + name + "/age:" + age);
}

public static void main(String[] args)
{
String[] source =new String[]{
"",
"abc",
"{}",
"{\"nick_name\":\"Jack\"}",
"{\"name\":\"Anna\",\"id\":5}",
"{\"name\":\"Pluto\",\"age\":15,\"id\":10}"
};

Gson g=new Gson();
TestJson x=null;
StringBuffer info=null;
for(int i=0;i {
info=new StringBuffer();
info.append(i).append(":").append(source[i]).append("\n\t");
try
{
x=g.fromJson(source[i], TestJson.class);
info.append((x == null) ? "NULL" : x.show());
}catch(Exception e)
{
info.append(e.getClass().getName());
}
System.out.println(info.toString());
}
}
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯