2016-12-16 102 views
0

我需要將以下Json代碼轉換爲Java。在Java中格式化Json代碼

{ 
    "service": { 
     "type": "nyd", 
     "discount": 0.20, 
     "items": [ 
      { 
       "asin": "....", 
       "link": "http://amazon.com/.....", 
       "quantity": 2 
      }, 
      // ... 
     ], 
     // See /addresses 
     "shipping_address": { 
      "full_name": "Mr Smith", 
      "street1": "Some Mission St", 
      "street2": "", // Optional 
      "city": "San Francisco", 
      "state": "CA", 
      "zip": "94000", 
      "country": "US", 
      "phone": "1234567890" 
     } 
    } 
} 

我目前使用下面的代碼實現這一點:

String  postUrl  = "https://API.example.com"; 
    Gson   gson   = new Gson(); 
    HttpClient httpClient = HttpClientBuilder.create().build(); 


    HttpPost  httpPostRequest = new HttpPost(postUrl); 

      StringEntity postingString = new StringEntity("{\"service\" : {\"type\":\"nnn\", \"discount\":" + 0.2 + ",\"items\" : [ { \"asin\":\"B018Y1XXT6\", \"link\":\"http://rads.stackoverflow.com/amzn/click/B018Y1XXT6", \"quantity\":" + 1 + " } ], \"shipping_address\" : {\"full_name\":\"Steven Smith\", \"street1\":\"11 Man Rd\", \"street2\":\"\", \"city\":\"Woonsocket\", \"state\":\"RI\", \"zip\":\"02844\", \"country\":\"US\", \"phone\":\"7746536483\" } } } "); 


    Mote: the values are different but I'm trying to achieve the same syntax. 


    System.out.println("Post String value: " + IOUtils.toString(postingString.getContent())); 



    httpPostRequest.addHeader("Authorization", "Token " + apiKey); 

    httpPostRequest.setEntity(postingString); 
    httpPostRequest.setHeader("Content-type", "application/json"); 
    //httpPostRequest.addHeader("content-type", "application/x-www-form-urlencoded"); 

    HttpResponse response = httpClient.execute(httpPostRequest); 

    System.out.println(response.toString()); 

的 「postString」 值是:

{"service" : {"type":"nnn", "discount":0.2,"items" : [ { "asin":"B018Y1XXT6", "link":"http://rads.stackoverflow.com/amzn/click/B018Y1XXT6", "quantity":1 } ], "shipping_address" : {"full_name":"Steven Smith", "street1":"11 Man Rd", "street2":"", "city":"Woonsocket", "state":"RI", "zip":"02844", "country":"US", "phone":"17746536483" } } } 

然而,當我試圖提交請求我得到一個錯誤的請求錯誤。

如何正確格式化字符串?

感謝

+0

'服務='看起來對我來說很可疑。服務是json對象的一部分,所以我認爲你的StringEntity應該以'{\「service \」開始:'並且在末尾有相應的終止大括號。好像postString的值應該是'{「service」:{「type」:「nyd」,「....' – DaveH

回答

0

,如果你想要去提供。繼JSON您發送了一個不正確的JSON字符串是錯誤的:

1)「服務= {\」類型\」應該是{ \ 「服務\」:{\ 「型

2)折扣不應該是一個字符串 \」 折扣\ 「:\」 0.2 \ 「應該\ 」折扣\「:」 + 0.2 +「 ,

3)項= [應該\ 「項目\」:[

4)量不應該串 \ 「量\」:\ 「1 \」}應該\ 「量\」: 「+ 1 + 」}

5)逗號送貨地址鍵 ] shipping_address之前丟失=應] \「 shipping_address \」:

6)增加一個}在端

+0

謝謝!我根據你寫的內容修改了Json字符串:{」service「:{ 「type」:「nyd」,「discount」:0.2,「items」:[{「asin」:「B018Y1XXT6」,「link」:「https://www.amazon.com/Yubico-Y-159-YubiKey -4-Nano/dp/B018Y1XXT6 /「,」quantity「:1}],」shipping_address「:{」full_name「:」Steven Smith「,」street1「:」1221 Male Rd「,」street2「:」「, 「city」:「Woonsocket」,「state」:「RI」,「zip」:「02844」,「country」:「US」,「phone」:「17746536483」}}}但我仍然收到Bad Request錯誤 – JimmieK

+0

還有一件事,請在鏈接末尾刪除分號「amazon.com/Yubico-Y-159-YubiKey-4-Nano/dp/B018Y1XXT6 /」**; **, – Nilu

+0

還要重新檢查您的網址,Auth Values並使用response.getStatusLine()檢查確切的響應代碼getStatusCode() – Nilu