解析json . tojson (obj)给出null



w当我将本地-内部类ShipAddress的对象传递给Gson类的toJson()方法时,解析它时会返回null。

public class CrusialDateRest {
  public String getShippingAddressesDetails() {
    Gson gson = new GsonBuilder().create();
    try {
       Collection<ImplAddress> savedAddressBeans = new ArrayList<ImplAddress>();
       Collection<CtFlexField> countryFields = new ArrayList<CtFlexField>();
       Collection<CtFlexField> debitorFields = new ArrayList<CtFlexField>();
       class ShipAddress{
           Collection<ImplAddress> savedAddressBean = new ArrayList<ImplAddress>();
           Collection<CtFlexField> countryField = new ArrayList<CtFlexField>();
           Collection<CtFlexField> debitorField = new ArrayList<CtFlexField>();
            ShipAddress( Collection<ImplAddress> savedAddressBeans, Collection<CtFlexField> countryFields,Collection<CtFlexField> debitorFields){
                savedAddressBean=savedAddressBeans;
                countryField=countryFields;
                debitorField=debitorFields;
            }
        }

         String addrId= XmlParser.getNodeValue(address, Statics.BUYFLOW_NAMESPACE, "AddressId");
         String addrStreet1 = XmlParser.getNodeValue(address, Statics.BUYFLOW_NAMESPACE, "AddrStreet1");
         String addrStreet2 = XmlParser.getNodeValue(address, Statics.BUYFLOW_NAMESPACE, "AddrStreet2");    
         String addrStreet3 = XmlParser.getNodeValue(address, Statics.BUYFLOW_NAMESPACE, "AddrStreet3");
        ImplAddress impladdress = new ImplAddress();
        impladdress.setAddressId(addrId);
        impladdress.setAddrStreet1(addrStreet1);
        impladdress.setAddrStreet2(addrStreet2);
        impladdress.setAddrStreet3(addrStreet3);
        savedAddressBeans.add(impladdress);
        }
     CtFlexField[] flexField =  flexFields.getFlexField();
      for (CtFlexField flex : flexField) {
         if(flex.getBundle().equalsIgnoreCase("Countries")){
             countryFields.add(flex);
         }
         else if(flex.getBundle().equalsIgnoreCase("CommonBundle")){
             debitorFields.add(flex);
         }
     }
      jsonResponse = gson.toJson(new ShipAddress(savedAddressBeans,countryFields,debitorFields));
      OUT.debug("jsonResponse--"+jsonResponse);
    } catch (Exception e) {
      OUT.error("rest method getShippingAddresses error", e);
    }
    return jsonResponse;
  }
}

我应该让内部类外部方法吗?

或者这是一个序列化问题?

这很可能是可见性问题。

ShipAddress移动到自己的类文件中,或者使其成为public static内部类。

注意,公共静态类不能在方法内部声明,您必须将类移出getShippingAddressDetails()方法。

相关内容

  • 没有找到相关文章

最新更新