简体   繁体   English

JSON解析未使用Gson提供正确的值

[英]JSON parsing not giving correct values using Gson

Hi I tried to parse a JSON like: 嗨,我尝试解析JSON,例如:

{"error":{"code":20,"message":"Transaction not found."}}

The Code is used is : 使用的代码是:

GulfBoxError errordetails= new Gson().fromJson(json, GulfBoxError.class);
                System.out.println("RESULT   :"+errordetails.getCode()+"   "+errordetails.getMessage());

Class file is: 类文件是:

public class GulfBoxError {
public int code;
public String message;

public int getCode() {
    return code;
}
public String getMessage() {
    return message;
}
}

Every time i tries and i didn't get he value here: 每次我尝试但没有得到他的重视时:

RESULT   :0   null

Any Idea Why?? 知道为什么吗? What i am missing here 我在这里想念的

  • You don't need getters if you don't encapsulate fields. 如果不封装字段,则不需要getter。
  • You object is mailformed. 您的对象是邮件格式的。 Top level should contain only one field: error . 顶层应仅包含一个字段: error

The code should look like: 该代码应如下所示:

public class GufError{
    public GulfBoxError error;
}

public class GulfBoxError {
    public int code;
    public String message;

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }
}
GufError errordetails= new Gson().fromJson(json, GufError.class);

You can try this, In case you don't want to create a separate class as wrapper: 如果您不想创建一个单独的类作为包装器,可以尝试一下:

Gson gson = new Gson();
JsonObject jsonObj = gson.fromJson(json,JsonObject.class);
GulfBoxError errordetails= gson.fromJson(jsonObj.get("error"), GulfBoxError.class);
System.out.println("RESULT   :"+errordetails.getCode()+"   "+errordetails.getMessage());

Your GulfBoxError class isn't correct. 您的GulfBoxError类不正确。

You need something like this: 您需要这样的东西:

 public class GulfError{
    public GulfBoxError error;
}

 class GulfBoxError {
    public int code;
    public String message;

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }
}

And parse it in this way: 并以这种方式解析:

    Gson gson = new Gson();
    String filename="/...Pathtoyour/json.json";
    JsonReader reader = new JsonReader(new FileReader(filename));
    GulfError errordetails= gson.fromJson(reader, GulfError.class);
    System.out.print("errordetails: " + gson.toJson(errordetails));

Anyway, if you want to use your GulfBoxError class, you could do this: 无论如何,如果您想使用GulfBoxError类,则可以这样做:

      Type listType = new TypeToken<Map<String, GulfBoxError>>(){}.getType();
      Map<String, GulfBoxError> mapGulfBoxError= gson.fromJson(reader,listType);
      for (Map.Entry<String, GulfBoxError> entry : mapGulfBoxError.entrySet())
      {
          System.out.println("Key: " + entry.getKey() + "\nValue:" + gson.toJson(entry.getValue()));

      }

Sometimes this could be useful if you don't want to create exactly the object that represent your Json. 有时,如果您不想创建代表Json的对象,这可能会很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM