2016-08-03 24 views
0

JSON:如何將嵌套JSON寫入HashMap <String,String>?

{ 
    "deviceId": "AAAAAAA1", 
    "cardInfo": { 
     "pan": "123456789", 
     "psn": "00", 
     "cvv": "123", 
     "panExpiryDate": "2017-12-12" 
    }, 
    "productType": "CREDIT", 
    "requestor": 1234, 
    "aid": "A0000000", 
    "aidVersion": 1, 
    "panSource": null, 
    "deviceLanguage": "en" 
} 

的Android代碼:

protected String doInBackground(Void... params) { 

    Bundle bundle=getIntent().getExtras(); 
    final String newdata=bundle.getString("newdata"); 

    try { 
     js=new JSONObject(newdata); 
     di=js.getString("deviceId"); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

    postDataParams = new HashMap<String, String>(); 
    postDataParams.put("deviceId",deviceID); 
    postDataParams.put("cardInfo.pan",card); 
    postDataParams.put("cardInfo.psn",Psn); 
    postDataParams.put("cardInfo.cvv",cvv); 
    postDataParams.put("cardInfo.panExpiryDate",panExpiryDate); 
    postDataParams.put("productType",productType); 
    postDataParams.put("requestor",requestor); 
    postDataParams.put("aid",aid); 
    postDataParams.put("aidVersion",aidVersion); 
    postDataParams.put("panSource",panSource); 
    postDataParams.put("deviceLanguage",deviceLanguage); 

    response = service.postServerData(path,postDataParams); 

    try { 
     json = new JSONObject(response); 
     System.out.println("success " + json.get("success")); 
     success = json.getInt("success"); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
    return response; 
} 

在編寫這些代碼我沒有得到從URL中任何迴應。你能幫我解決我犯的錯誤嗎?

+3

「給我一個完整的代碼」不會在這裏工作的#1,這是一個問題和答案的地方,而不是要求別人代碼爲你免費。請描述您的問題,添加您正在努力的相關代碼,並希望有人能夠提供幫助。 – Egor

+0

你希望得到的'HashMap'的鍵是什麼格式?什麼對您發佈的代碼無效或不完整? – qxz

+0

你需要帶有字符串對象類型的hasmap,並且在該hashmap中放置一個帶有密鑰cardInfo的json對象。 –

回答

0

更改此

postDataParams.put("cardInfo.pan",card); 
postDataParams.put("cardInfo.psn",Psn); 
postDataParams.put("cardInfo.cvv",cvv); 
postDataParams.put("cardInfo.panExpiryDate",panExpiryDate); 

這個

JSONObject cardInfoJson = new JSONObject(); 
cardInfoJson.put("pan", card); 
cardInfoJson.put("psn", psn); 
cardInfoJson.put("cvv", cvv); 
cardInfoJson.put("panExpiryDate", panExpiryDate); 
postDataParams.put("cardInfo", cardInfoJson); 
+0

對不起,它不工作..我可以在哪裏發送完整的代碼?你能告訴我嗎? –

相關問題