简单的Json到使用GSON进行Java转换 [英] Simple Json to Java convertion using GSON

查看:122
本文介绍了简单的Json到使用GSON进行Java转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用GSON库来反序列化来自Web服务的JSON,但我无法使其工作。我决定在一些简单的输入上测试GSON - 不能使它更简单,但它仍然不起作用。我查看了类似的问题,如将JSON转换为Java ,所有这些都表明了类似的方法解决方案。我的猜测是我错过了一些非常简单明显的东西,因此对代码的全新观点可能会有所帮助。
所以这是我的:

JSON

  {A:{name:qwrety,value1:1,value2:2}} 

Java类

  import com.google.gson.Gson; 

public class Main {

public static void main(String [] args){
String json = / *从服务器获取JSON * /
Gson gson = new Gson();
A obj = gson.fromJson(json,A.class);
System.out.println(obj);
}
}

class A {
私人字符串名称;
private int value1;
private int value2;

public String getName(){return name; }
public int getValue1(){return value1; }
public int getValue2(){return value2; }

public void setName(String name){this.name = name; }
public void setValue1(int value1){this.value1 = value1; }
public void setValue2(int value2){this.value2 = value2; }
$ b $ public String toString(){
return String.format(name:%s,value1:%d,value2:%d,name,value1,value2);


我得到的回报是

  name:null,value1:0,value2:0 

任何人都可以知道这段代码有什么问题吗?



UPD
As @torbinsky和@ Kevin-Dolan指出,问题在于Java类结构与Json格式不匹配。为了解决这个问题,我添加了一个新类

  class Container {
private A a;

public getA(){return a; }

public void setA(A a){this.a = a; }






将反序列化调用更改为

  Gson gson = new Gson(); 
Container obj = gson.fromJson(json,Container.class);

System.out.println(obj.getA());

然而,无论如何我都会打印出null

解决方案

我认为您的问题是您的JSON字符串。



尝试更改:

  {A:{name:qwrety,value1:1,value2:2}} 
  {name :qwrety,value1:1,value2:2} 

编辑:
我认为问题可能是您的JSON字符串有A,并且区分大小写。尝试将您的成员变量更改为大写字母A。对不起,我目前无法测试以确认这一点。

如果JSON格式已修复,请尝试将您的类更改为类似的内容:

  class Foo {
private String name;
private int value1;
private int value2;

public String getName(){return name; }
public int getValue1(){return value1; }
public int getValue2(){return value2; }

public void setName(String name){this.name = name; }
public void setValue1(int value1){this.value1 = value1; }
public void setValue2(int value2){this.value2 = value2; }
$ b $ public String toString(){
return String.format(name:%s,value1:%d,value2:%d,name,value1,value2);


$ / code $ / pre
$ b $ p


  class Container {
private Foo A;

public Foo getA(){return A; }

public void setA(Foo A){this.A = A; }
}


I've recently started using GSON library for deserializing JSON that comes from web service but I can't make it work. I decide to test GSON on some simple input - can't make it any simpler and it still doesn't work. I've looked through similar issues like Converting JSON to Java, all of which suggest similar approach to solution. My guess is that I'm missing something really simple and obvious so a fresh view on the code would probably help. So here is what I have:

JSON

{"A":{"name":"qwrety","value1":1,"value2":2}}

Java class

import com.google.gson.Gson;

public class Main {

    public static void main(String[] args) {        
        String json = /*getting JSON from server*/       
        Gson gson = new Gson();
        A obj = gson.fromJson(json, A.class);
        System.out.println(obj);
    }
}

class A {
    private String name;
    private int value1;
    private int value2;

    public String getName() { return name; }
    public int getValue1() { return value1; }
    public int getValue2() { return value2; }

    public void setName(String name) { this.name = name; }
    public void setValue1(int value1) { this.value1 = value1; }
    public void setValue2(int value2) { this.value2 = value2; }

    public String toString() {
        return String.format("name: %s, value1: %d, value2: %d", name, value1, value2);
    }
}

What I get in return is

name: null, value1: 0, value2: 0

Anyone can tell what is wrong with this code?

UPD As @torbinsky and @Kevin-Dolan pointed out, the problem was because Java class structure didn't match the Json format. To fix this I added a new class

class Container {
    private A a;

    public A getA() { return a; }

    public void setA(A a) { this.a = a; }

}

and changed the deserialisation call to following

Gson gson = new Gson();
Container obj = gson.fromJson(json, Container.class);

System.out.println(obj.getA());

However I anyway get "null" printed out

解决方案

I think your problem is your JSON string.

Try changing:

{"A":{"name":"qwrety","value1":1,"value2":2}}

to:

{"name":"qwrety","value1":1,"value2":2}

EDIT: I think the problem might be your JSON string has "A" and that is case-sensitive. Try changing your member variable to an uppercase "A". Sorry I am unable to test at the moment to confirm this.

If the JSON format is fixed then try changing your classes to something similar:

class Foo {
private String name;
private int value1;
private int value2;

public String getName() { return name; }
public int getValue1() { return value1; }
public int getValue2() { return value2; }

public void setName(String name) { this.name = name; }
public void setValue1(int value1) { this.value1 = value1; }
public void setValue2(int value2) { this.value2 = value2; }

public String toString() {
    return String.format("name: %s, value1: %d, value2: %d", name, value1, value2);
}
}

and:

class Container {
private Foo A;

public Foo getA() { return A; }

public void setA(Foo A) { this.A = A; }
}

这篇关于简单的Json到使用GSON进行Java转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆