【问题标题】:How do i write nested JSON into HashMap<String,String>?如何将嵌套 JSON 写入 HashMap<String,String>?
【发布时间】:2016-08-03 09:07:16
【问题描述】:

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"
} 

安卓代码:

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 的任何响应。你能帮我在哪里做错吗?

【问题讨论】:

  • “给我发送完整的代码”在 Stackoverflow 上不起作用,这里是提问和回答的地方,而不是让其他人免费为您编码。请描述您的问题,添加您正在努力解决的相关代码,希望有人能够提供帮助。
  • 您希望生成的HashMap 的键是什么格式?您发布的代码有哪些不起作用或不完整的地方?
  • 您需要获取字符串对象类型的hasmap。在该hashmap中放入一个带有key cardInfo的json对象。
  • 已添加代码....请帮助

标签: android json hashmap


【解决方案1】:

改变这个

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);

【讨论】:

  • 对不起,它不工作.. 我在哪里可以给你完整的代码?请告诉我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-05
  • 2021-12-12
  • 1970-01-01
  • 2018-07-30
相关资源
最近更新 更多