简体   繁体   English

java bean布尔值提交到json&boolean vs Boolean

[英]java bean boolean filed to json & boolean vs Boolean

I met a problem that is , when I use smart-json to convert java bean to JSON Object, then i found some boolean filed was lost, can some boby tell me why? 我遇到了一个问题,当我使用smart-json将java bean转换为JSON Object时,我发现丢失了一些布尔值字段,有些boby可以告诉我为什么吗? the who codes are below: 谁的代码如下:

package com.huoli.crawler.test;

import java.util.HashMap;
import java.util.Map;

import net.minidev.json.JSONObject;


public class MiniDevJSONTest {
public static void main(String[] args) {
    MyBean mybean = new MyBean();
    mybean.setReturn(true);
    mybean.setArrivingAirportCode("dadsa");
    Map<String, MyBean> map = new HashMap<>();
    map.put("mybean", mybean);
    // output smart-json:{"mybean":{"arrivingAirportCode":"dadsa"}}
    // so where is isRetrun ??
    System.out.println("smart-json:" + JSONObject.toJSONString(map));

}

} }

class MyBean {
private boolean isReturn;

public boolean isReturn() {
    return isReturn;
}

public void setReturn(boolean isReturn) {
    this.isReturn = isReturn;
}

private String arrivingAirportCode;

public String getArrivingAirportCode() {
    return arrivingAirportCode;
}

public void setArrivingAirportCode(String arrivingAirportCode) {
    this.arrivingAirportCode = arrivingAirportCode;
}

} }

My question is why the boolean field value was lost? 我的问题是为什么布尔字段值丢失了?

This is a getter versus "is"-er problem: 这是一个吸气与“是”的问题:

Modify your code in MyBean as I have the following snippet, and change the set of the boolean in your MiniDevJSONTest class to match "setIsReturn". 按照下面的代码片段,在MyBean中修改您的代码,并在您的MiniDevJSONTest类中更改布尔集,以匹配“ setIsReturn”。 You will now get the value you are looking for. 现在,您将获得所需的价值。 Not very familiar with the minidev.json classes, but there appears to be reflection going on underneath the covers that is looking for the getter for your boolean value and not the "is"-er. 对minidev.json类不是很熟悉,但是似乎正在寻找在寻找布尔值而不是“ is” -er的getter的底层。 Since it doesn't find it, it's like it's not there.. 由于找不到它,所以好像它不存在。

I have seen this kind of behavior before in other libraries. 我以前在其他库中已经看到过这种行为。 In some libraries the choice between whether the code seeks out the is-er or the getter changes based on whether or not the boolean you are looking for is the primitive or the fully boxed type. 在某些库中,代码是在查找iser还是在getter之间进行选择,这取决于您要查找的布尔值是原始类型还是完全装箱的类型。

public class  MyBean {
  private boolean isReturn;
  private String arrivingAirportCode;

  public boolean getIsReturn() {
    return isReturn;
  }

  public void setIsReturn(boolean isReturn) {
    this.isReturn = isReturn;
  }

  public String getArrivingAirportCode() {
    return arrivingAirportCode;
  }

  public void setArrivingAirportCode(String arrivingAirportCode) {
    this.arrivingAirportCode = arrivingAirportCode;
  }
}

Just a little more followup: 再跟进一点:

In eclipse, when you establish a class attribute for a class, if you use the "create getter and setter" shortcut, you will see that it automatically creates a getter for a big B Boolean, and a is-er for the primitive type. 在eclipse中,当为类建立类属性时,如果使用“ create getter and setter”快捷方式,则会看到它会自动为一个大B布尔值和一个is-er为原始类型创建一个getter。

Many libraries use this standard when trying to figure out the reflection pattern for examining a class. 许多库在尝试找出用于检查类的反射模式时都使用此标准。 However, it appears that the library you are using does not. 但是,看来您正在使用的库没有。 I tested it, and it is expecting the getter whether the attribute is a boxed type or the primitive. 我对其进行了测试,并且期望该getter属性是盒装类型还是原始类型。

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

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