0

任何人都知道 - 如何將JSONObject轉換爲POJO類?ibm mobilefirst適配器 - 將JSONObject轉換爲POJO類

我已經創建了一個適配器,我想在將它發送給客戶端之前將其轉換爲Pojo。

1)我ResourceAdapterResource.java(適配器)

@POST 
@Path("profiles/{userid}/{password}") 
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) 
public JSONObject getStatus(@PathParam("userid") String userid, @PathParam("password") String password) throws IOException { 

    Map<String, Object> maps = new HashMap<String,Object>(); 
      map.put("userid", userid); 
      map.put("password",password); 

    // json Object will get the value from SOAP Adapter Service 
    JSONObject obj = soapAdapterService(maps); 

    /** Question here, how to add to POJO.. I have code here but not work, null values**/ 
     // set to Object Pojo Employee 
    Employee emp = new Employee(); 
    emp.setUserId(String.valueOf(obj.get("userId"))); 
    emp.setUserStatus((String.valueOf(obj.get("userStatus"))); 

    // when I logging its show Empty. 
    logger.info("User ID from service : " + emp.getUserId()); 
    logger.info("Status Id from service : " + emp.getUserStatus()); 


    return obj; 
} 

2.)POJO類 - 僱員

import java.io.Serializable; 

@SuppressWarnings("serial") 
public class Employee implements Serializable{ 
private String userid; 
private String userStatus; 

public String getUserid() { 
     return userid; 
    } 

    public void setUserid(String userid) { 
     this.userid= userid; 
    } 

    public String getUserStatus() { 
     return userStatus; 
    } 

    public void setUserStaus(String userStatus) { 
     this.userStatus= userStatus; 
    } 
} 

當我測試使用揚鞭 - MobileFirst控制檯寧靜測試,它返回的JSONObject與使用服務中的數據成功返回Body。

但是,當我檢查日誌信息(message.log) - 服務器日誌,狀態爲空。從服務

用戶ID:空 狀態標識從服務:空

似乎它的JSON的Java IBM API,它有ObjectMapper像傑克遜API映射到JSONObject的POJO類。

  • 結果從揚鞭
  • { 
         "statusReason": "OK", 
         "responseHeaders": { 
         "Content-Length": "1849", 
         "Content-Language": "en-US", 
         "Date": "Thu, 23 Mar 2017 01:40:33 GMT", 
         "X-Powered-By": "Servlet/3.0", 
         "Content-Type": "text/xml; charset=utf-8" 
         }, 
         "isSuccessful": true, 
         "responseTime": 28, 
         "totalTime": 33, 
         "warnings": [], 
         "Envelope": { 
         "soapenv": "http://schemas.xmlsoap.org/soap/envelope/", 
         "Body": { 
          "checkEmployeeLoginResponse": { 
          "a": "http://com.fndong.my/employee_Login/", 
          "loginEmployeeResp": { 
           "Employee": { 
           "idmpuUserName": "fndong", 
           "Status": "A", 
           "userid": "fndong", 
           "Password": "[email protected]=", 
           "PwdCount": "1", 
           "rEmail": "[email protected]" 
           }, 
           "sessionId": "%3F", 
           "statusCode": "0" 
          } 
          } 
         } 
         }, 
         "errors": [], 
         "info": [], 
         "statusCode": 200 
        } 
    

    然後我跟着你的建議投爲String:

    String objUserId = (String) objectAuth.get("userid"); 
    

    結果還是空,它需要通過調用正文函數「loginEmployeeResp」來指示json restful結果,因爲數據JSon Object來自服務SOAP。

    回答

    1

    顯然你的String.valueOf(obj.get("userId"))返回null或空,所以問題是,它的哪一部分?

    您可以登錄obj.get("userId")並查看是否爲空,在這種情況下,響應不包含您的期望。

    但我懷疑問題是String.valueOf()轉換沒有做你期望的。它看起來像在MobileFirst的JSONObjectcom.ibm.json.java.JSONObject,當我在搜索時,example I found只是強制轉換爲String

    emp.setUserId((String) obj.get("userId")); 
    

    編輯:現在你已經添加了揚鞭結果,我說你的obj.get("userId")本身可能返回null。你檢查了嗎?

    首先,「userId」不是「userid」。大寫字母很重要。

    但更重要的是,「userid」嵌套在JSON的深處,所以我不認爲只是從頂層JSONObject開始工作。我認爲你必須做一些事情,如:

    JSONObject envelope = (JSONObject) obj.get("Envelope"); 
    JSONObject body = (JSONObject) envelope.get("Body"); 
    JSONObject response = (JSONObject) body.get("checkEmployeeLoginResponse"); 
    JSONObject resp = (JSONObject) response.get("loginEmployeeResp"); 
    JSONObject employee = (JSONObject) resp.get("Employee"); 
    emp.setUserId((String) employee.get("userid")); 
    emp.setUserStatus((String) employee.get("status")); 
    

    (遺憾的是,與特定的IBM JSON4J,我不認爲有辦法做到JSON的更自動化的解組到Java對象。)

    +0

    嗨dbreaux,我做了這麼多類型的演員,結果仍然是空的。我上面編輯了我的問題,我認爲它總是顯示null,因爲json格式restful與示例中的函數名稱不同。 – fndong