如何将嵌套的JSON写入HashMap <string,string>?

时间:2016-08-03 09:07:17

标签: android json hashmap

JSON:

{
    "deviceId": "AAAAAAA1",
    "cardInfo": {
        "pan": "123456789012345",
        "psn": "00",
        "cvv": "123",
        "panExpiryDate": "2017-12-12"
    },
    "productType": "CREDIT",
    "requestor": 1234,
    "aid": "A000000001234567",
    "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;
}

在编写此代码时,我没有收到来自网址的任何回复。你可以帮我解决我的错误。

1 个答案:

答案 0 :(得分: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);