2016-08-03 6 views
0

JSON:Wie schreibe ich verschachtelten JSON in 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 Code:

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

Auf diesen Code zu schreiben ist keine Antwort von der URL zu bekommen. Kannst du mir bitte helfen, wo ich den Fehler gemacht habe?

+3

„Schicken Sie mir einen vollständigen Code“ wird hier nicht auf Stackoverflow arbeiten, dies ist ein Ort für Fragen und Antworten ist, für andere nicht zu fragen Code für Sie kostenlos. Bitte beschreiben Sie Ihr Problem, fügen Sie den relevanten Code hinzu, mit dem Sie zu kämpfen haben, und hoffentlich wird Ihnen jemand helfen können. – Egor

+0

In welchem ​​Format sollen die Schlüssel der resultierenden 'HashMap' liegen? Was funktioniert nicht oder ist unvollständig über den Code, den Sie gepostet haben? – qxz

+0

Sie müssen eine hasmap des string-Objekttyps nehmen. Und in diesem hashmap ein json-Objekt mit key cardInfo einfügen. –

Antwort

0

ändern Sie diese

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

diesen

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

Sorry es funktioniert nicht .. Wo kann ich Ihnen den vollständigen Code senden? Kannst du es bitte erzählen? –