如何使用 GSON 正确解析这个 json 字符串。 JsonObject 返回空,没有解析错误

发布于 2024-11-09 17:10:03 字数 1239 浏览 0 评论 0原文

我的数据结构如下

{"testString":"some string","success":true,"reason":null,"data":{"networks":[{"networkId":"1","networkName ":"some area","networkType":1},{"networkId":"4","networkName":"another place","networkType":1}]}}

所以基本上它是一个数组对象,第一个你键/值对是 1)String 2)Bool 3)String,第四个是 JSONObject

我用作所有这些数据的容器的对象是

public class ContainerData {


private boolean success;
private String reason;

private JSONObject data;

private String testString;

public String getTestString(){
    return this.testString;
}
public void setTestString(String test){
    this.testString = test;
}

public boolean getSuccess() {
    return this.success;
}

public void setSuccess(boolean success) {
    this.success = success;
}

public String getReason() {
    return this.reason;
}

public void setReason(String reason) {
    this.reason = reason;
}

public JSONObject getData() {
    return this.data;
}

public void setData(JSONObject data) {
    this.data = data;
}
}

我像这样解析它,响应对象是

  Gson gson = new GsonBuilder().serializeNulls().create();
  responseObject = gson.fromJson(response,ContainerData.class);

我可以获得的 json 字符串除了 JSONObject 之外的所有值,它总是返回一个空数组,例如 {} 对我搞砸的地方有什么想法吗?

I have a data structure as follows

{"testString":"some string","success":true,"reason":null,"data":{"networks":[{"networkId":"1","networkName":"some area","networkType":1},{"networkId":"4","networkName":"another place","networkType":1}]}}

So basically it's an array object with the first thee key/value pairs being a 1)String 2)Bool 3)String and the fourth is a JSONObject

The Object i'm using as a container for all this data is

public class ContainerData {


private boolean success;
private String reason;

private JSONObject data;

private String testString;

public String getTestString(){
    return this.testString;
}
public void setTestString(String test){
    this.testString = test;
}

public boolean getSuccess() {
    return this.success;
}

public void setSuccess(boolean success) {
    this.success = success;
}

public String getReason() {
    return this.reason;
}

public void setReason(String reason) {
    this.reason = reason;
}

public JSONObject getData() {
    return this.data;
}

public void setData(JSONObject data) {
    this.data = data;
}
}

I parse it like this withr esponse object being the json string

  Gson gson = new GsonBuilder().serializeNulls().create();
  responseObject = gson.fromJson(response,ContainerData.class);

I can get values for everything except the JSONObject it always returns an empty array like {} Any thoughts on where I'm messing up?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

骑趴 2024-11-16 17:10:03

这是一种使用您提供的 JSON 输入的方法,但更“完全”反序列化为友好的数据结构。

输出是

[ContainerData: success=true, reason=null, testString=some string, data=[Data: networks=[
[Network: networkId=1, networkName=some area, networkType=1], 
[Network: networkId=4, networkName=another place, networkType=1]
]]]
public class Foo
{
  static String jsonInput = 
    "{" + 
      "\"testString\":\"some string\"," + 
      "\"success\":true," + 
      "\"reason\":null," + 
      "\"data\":" + 
      "{" + 
        "\"networks\":" + 
        "[" + 
          "{\"networkId\":\"1\",\"networkName\":\"some area\",\"networkType\":1}," + 
          "{\"networkId\":\"4\",\"networkName\":\"another place\",\"networkType\":1}" + 
        "]" + 
      "}" + 
    "}";

  public static void main(String[] args)
  {
    GsonBuilder gsonBuilder = new GsonBuilder();
    Gson gson = gsonBuilder.create();
    ContainerData container = gson.fromJson(jsonInput, ContainerData.class);
    System.out.println(container);
  }
}

class ContainerData
{
  private boolean success;
  private String reason;
  private String testString;
  private Data data;

  @Override
  public String toString()
  {
    return String.format(
        "[ContainerData: success=%1$b, reason=%2$s, testString=%3$s, data=%4$s]", 
        success, reason, testString, data);
  }
}

class Data
{
  private Network[] networks;

  @Override
  public String toString()
  {
    return String.format(
        "[Data: networks=%1$s]", 
        Arrays.toString(networks));
  }
}

class Network
{
  private String networkId;
  private String networkName;
  private int networkType;

  @Override
  public String toString()
  {
    return String.format(
        "[Network: networkId=%1$s, networkName=%2$s, networkType=%3$d]",
        networkId, networkName, networkType);
  }
}

Here's an approach that works with the JSON input you provided, but more "fully" deserializes into a friendly data structure.

The output is

[ContainerData: success=true, reason=null, testString=some string, data=[Data: networks=[
[Network: networkId=1, networkName=some area, networkType=1], 
[Network: networkId=4, networkName=another place, networkType=1]
]]]
public class Foo
{
  static String jsonInput = 
    "{" + 
      "\"testString\":\"some string\"," + 
      "\"success\":true," + 
      "\"reason\":null," + 
      "\"data\":" + 
      "{" + 
        "\"networks\":" + 
        "[" + 
          "{\"networkId\":\"1\",\"networkName\":\"some area\",\"networkType\":1}," + 
          "{\"networkId\":\"4\",\"networkName\":\"another place\",\"networkType\":1}" + 
        "]" + 
      "}" + 
    "}";

  public static void main(String[] args)
  {
    GsonBuilder gsonBuilder = new GsonBuilder();
    Gson gson = gsonBuilder.create();
    ContainerData container = gson.fromJson(jsonInput, ContainerData.class);
    System.out.println(container);
  }
}

class ContainerData
{
  private boolean success;
  private String reason;
  private String testString;
  private Data data;

  @Override
  public String toString()
  {
    return String.format(
        "[ContainerData: success=%1$b, reason=%2$s, testString=%3$s, data=%4$s]", 
        success, reason, testString, data);
  }
}

class Data
{
  private Network[] networks;

  @Override
  public String toString()
  {
    return String.format(
        "[Data: networks=%1$s]", 
        Arrays.toString(networks));
  }
}

class Network
{
  private String networkId;
  private String networkName;
  private int networkType;

  @Override
  public String toString()
  {
    return String.format(
        "[Network: networkId=%1$s, networkName=%2$s, networkType=%3$d]",
        networkId, networkName, networkType);
  }
}
绝不服输 2024-11-16 17:10:03

我已经解析了你上面提到的 JSON 字符串。它没有使用 GSON 来解析 JSON。请也尝试一下这个。我认为这会解决你的问题:

String jsonString = "{\"testString\":\"some string\",\"success\":true,\"reason\":null,\"data\":{\"networks\":[{\"networkId\":\"1\",\"networkName\":\"some area\",\"networkType\":1},{\"networkId\":\"4\",\"networkName\":\"another place\",\"networkType\":1}]}}";

                JSONObject jObject = new JSONObject(jsonString); 

                String menuObject = jObject.getString("testString");
                System.out.println("testString="+menuObject);

                String menuObject1 = jObject.getString("success");
                System.out.println("success="+menuObject1);

                String menuObject2 = jObject.getString("reason");
                System.out.println("reason="+menuObject2);

                String menuObject3 = jObject.getString("data");
                System.out.println("data="+menuObject3);

                JSONObject popupObject = jObject.getJSONObject("data");

                JSONArray jObject1 = popupObject.getJSONArray("networks"); 
                for (int i = 0; i < 2; i++) {
                    System.out.println("networkId of"+" "+i+" th element= "+jObject1.getJSONObject(i).getString("networkId").toString());
                    System.out.println("networkName of"+" "+i+" th element= "+jObject1.getJSONObject(i).getString("networkName").toString());
                    System.out.println("networkType of"+" "+i+" th element= "+jObject1.getJSONObject(i).getString("networkType").toString());

                }

I have parsed the JSON string you have mentioned above. It is not using the GSON to parse the JSON. Please try this also. I think it will sove your issue:

String jsonString = "{\"testString\":\"some string\",\"success\":true,\"reason\":null,\"data\":{\"networks\":[{\"networkId\":\"1\",\"networkName\":\"some area\",\"networkType\":1},{\"networkId\":\"4\",\"networkName\":\"another place\",\"networkType\":1}]}}";

                JSONObject jObject = new JSONObject(jsonString); 

                String menuObject = jObject.getString("testString");
                System.out.println("testString="+menuObject);

                String menuObject1 = jObject.getString("success");
                System.out.println("success="+menuObject1);

                String menuObject2 = jObject.getString("reason");
                System.out.println("reason="+menuObject2);

                String menuObject3 = jObject.getString("data");
                System.out.println("data="+menuObject3);

                JSONObject popupObject = jObject.getJSONObject("data");

                JSONArray jObject1 = popupObject.getJSONArray("networks"); 
                for (int i = 0; i < 2; i++) {
                    System.out.println("networkId of"+" "+i+" th element= "+jObject1.getJSONObject(i).getString("networkId").toString());
                    System.out.println("networkName of"+" "+i+" th element= "+jObject1.getJSONObject(i).getString("networkName").toString());
                    System.out.println("networkType of"+" "+i+" th element= "+jObject1.getJSONObject(i).getString("networkType").toString());

                }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文