簡體   English   中英

在解析gson.toJson(obj)給出null

[英]On parsing gson.toJson(obj) giving 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()方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM