2017-01-27 96 views
1

我正在Java中實現一個api調用,唯一提供的例子是在PHP中,我的問題什麼是json_encode的Java等價物,所以我們得到完全相同的輸出。什麼是Java相當於php json_encode

foreach($products_from_order as $item_id => $item) 
    { 
     $value[] = array(
      'product_sku' =>$item['sku'], 
      'product_name' =>$item['name'], 
      'product_desc' =>$item['description'], 
      'product_price' =>$item['price'], 
      'product_url' =>$item['url'], 
      'product_image' => $item['image'], 
     ); 
    } 

    $apiData = array("merchant_id"  => $merchantId, 
     "order_id"   => $orderId, 
     "customer_name"  => $customerName, 
     "customer_email" => $customerEmail, 
     "purchase_date"  => $purchaseDate, 
     "products"   => $value, 
     "key"    => $apiKey, 
     "hmac"    => $calculatedHmac 
    ); 
    $data_string = json_encode($apiData) 

我現在有一個示例輸出,所以我想我只需要知道要做到這一點在Java中

{ 
    "merchant_id":"526", 
    "order_id":"15", 
    "customer_name":"Tom Tester", 
    "customer_email":"[email protected]", 
    "purchase_date":"2017-01-27 22:05:56", 
    "key":"24e694753d1c6d957882d4d560902b3f05454a0357b4c73a5bc66469653644cf9374ed5de8a2781d4151732299e821c4adf726f884923a46ae60a43d1b197164", 
    "hmac":"kH9BBiRPMdHTP\\/dZgcUCPaldhpL9ttUw+lYl7LdB32Q=", 
    "products":[ 
     { 
     "product_sku":"47", 
     "product_name":"HP LP3065", 
     "product_desc":"\\r\\n\\tStop your co-workers in their tracks with the stunning new 30-inch diagonal HP LP3065 Flat Panel Monitor. This flagship monitor features best-in-class performance and presentation features on a huge wide-aspect screen while letting you work as comfortably as possible - you might even forget you're at the office\\r\\n..", 
     "product_price":"100.0000", 
     "product_url":"http:\\/\\/ts.oc-develop.com\\/index.php?route=product\\/product&product_id=47", 
     "product_image":"http:\\/\\/ts.oc-develop.com\\/image\\/cache\\/catalog\\/demo\\/hp_1-500x500.jpg" 
     }, 
     { 
     "product_sku":"41", 
     "product_name":"iMac", 
     "product_desc":"\\r\\n\\tJust when you thought iMac had everything, now there\\u00b4s even more. More powerful Intel Core 2 Duo processors. And more memory standard. Combine this with Mac OS X Leopard and iLife \\u00b408, and it\\u00b4s more all-in-one than ever. iMac packs amazing performance into a stunningly slim space.\\r\\n..", 
     "product_price":"100.0000", 
     "product_url":"http:\\/\\/ts.oc-develop.com\\/index.php?route=product\\/product&product_id=41", 
     "product_image":"http:\\/\\/ts.oc-develop.com\\/image\\/cache\\/catalog\\/demo\\/imac_1-500x500.jpg" 
     }, 
     { 
     "product_sku":"28", 
     "product_name":"HTC Touch HD", 
     "product_desc":"\\r\\n\\tHTC Touch - in High Definition. Watch music videos and streaming content in awe-inspiring high definition clarity for a mobile experience you never thought possible. Seductively sleek, the HTC Touch HD provides the next generation of mobile functionality, all at a simple touch. Fully integrated with Windows Mobile Professional 6.1, ultrafast 3.5G, GPS, 5MP camera, plus lots more - all delivered on a breathtakingly crisp 3.8" WVGA touchscreen - you can take control of your mobile world wi..", 
     "product_price":"100.0000", 
     "product_url":"http:\\/\\/ts.oc-develop.com\\/index.php?route=product\\/product&product_id=28", 
     "product_image":"http:\\/\\/ts.oc-develop.com\\/image\\/cache\\/catalog\\/demo\\/htc_touch_hd_1-500x500.jpg" 
     } 
    ] 
} 

更新 所以我與GSON庫

去的最簡單方法

爲訂單和個人創建POJO

import java.util.ArrayList; 
import java.util.List; 

public class Order 
{ 
    private String merchantId; 
    private String customerEmail; 
    private String customerName; 
    private String orderId; 
    private String purchaseDate; 
    private String key; 
    private String hmac; 
    private List<Product> products = new ArrayList(); 

    public String getCustomerEmail() 
    { 
     return customerEmail; 
    } 

    public void setCustomerEmail(String customerEmail) 
    { 
     this.customerEmail = customerEmail; 
    } 

    public String getCustomerName() 
    { 
     return customerName; 
    } 

    public void setCustomerName(String customerName) 
    { 
     this.customerName = customerName; 
    } 

    public String getOrderId() 
    { 
     return orderId; 
    } 

    public void setOrderId(String orderId) 
    { 
     this.orderId = orderId; 
    } 

    public String getPurchaseDate() 
    { 
     return purchaseDate; 
    } 

    public void setPurchaseDate(String purchaseDate) 
    { 
     this.purchaseDate = purchaseDate; 
    } 

    public String getMerchantId() 
    { 
     return merchantId; 
    } 

    public void setMerchantId(String merchantId) 
    { 
     this.merchantId = merchantId; 
    } 

    public String getKey() 
    { 
     return key; 
    } 

    public void setKey(String key) 
    { 
     this.key = key; 
    } 

    public String getHmac() 
    { 
     return hmac; 
    } 

    public void setHmac(String hmac) 
    { 
     this.hmac = hmac; 
    } 

    public List<Product> getProducts() 
    { 
     return products; 
    } 

    public void addProducts(Product product) 
    { 
     products.add(product); 
    } 
} 


public class Product 
{ 
    private String productSku; 
    private String productName; 
    private String productDescription; 
    private String productPrice; 
    private String productUrl; 
    private String productImage; 

    public String getProductSku() 
    { 
     return productSku; 
    } 

    public void setProductSku(String productSku) 
    { 
     this.productSku = productSku; 
    } 

    public String getProductName() 
    { 
     return productName; 
    } 

    public void setProductName(String productName) 
    { 
     this.productName = productName; 
    } 

    public String getProductDescription() 
    { 
     return productDescription; 
    } 

    public void setProductDescription(String productDescription) 
    { 
     this.productDescription = productDescription; 
    } 

    public String getProductPrice() 
    { 
     return productPrice; 
    } 

    public void setProductPrice(String productPrice) 
    { 
     this.productPrice = productPrice; 
    } 

    public String getProductUrl() 
    { 
     return productUrl; 
    } 

    public void setProductUrl(String productUrl) 
    { 
     this.productUrl = productUrl; 
    } 

    public String getProductImage() 
    { 
     return productImage; 
    } 

    public void setProductImage(String productImage) 
    { 
     this.productImage = productImage; 
    } 
} 

,然後把它稱爲如下:

Order order = new Order(); 
     order.setMerchantId(String.valueOf(526)); 
     order.setOrderId(String.valueOf(15)); 
     order.setCustomerName("Tom Tester"); 
     order.setCustomerEmail("[email protected]"); 
     order.setPurchaseDate("2017-01-27 22:05:56"); 
     order.setHmac("kH9AAiRPMdHTP/dZgcUCPaldhpL9ttUw+lYl7LdB32Q="); 

     Product p1 = new Product(); 
     p1.setProductSku(String.valueOf(47)); 
     p1.setProductName("HP LP3065"); 
     p1.setProductDescription("\r\nStop your co-workers in their tracks with the stunning new 30-inc"); 
     p1.setProductPrice("100.0000"); 
     p1.setProductUrl("http://ts.oc-develop.com/index.php?route=product/product&amp;product_id=47"); 
     p1.setProductImage("http://ts.oc-develop.com/image/cache/catalog/demo/hp_1-500x500.jpg"); 
     order.addProducts(p1); 
     Product p2 = new Product(); 
     p2.setProductSku(String.valueOf(41)); 
     p2.setProductName("iMac"); 
     p2.setProductDescription("\r\nJust when you thought iMac had everything,"); 
     p2.setProductPrice("100.0000"); 
     p2.setProductUrl("http://ts.oc-develop.com/index.php?route=product/product&amp;product_id=41"); 
     p2.setProductImage("http://ts.oc-develop.com/image/cache/catalog/demo/imac_1-500x500.jpg"); 
     order.addProducts(p2); 

     Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).setPrettyPrinting().create(); 
     System.out.println(gson.toJson(order)); 

一聲輸出

{ 
    "merchant_id": "526", 
    "customer_email": "[email protected]", 
    "customer_name": "Tom Tester", 
    "order_id": "15", 
    "purchase_date": "2017-01-27 22:05:56", 
    "hmac": "kH9AAiRPMdHTP/dZgcUCPaldhpL9ttUw+lYl7LdB32Q\u003d", 
    "products": [ 
    { 
     "product_sku": "47", 
     "product_name": "HP LP3065", 
     "product_description": "\r\nStop your co-workers in their tracks with the stunning new 30-inc", 
     "product_price": "100.0000", 
     "product_url": "http://ts.oc-develop.com/index.php?route\u003dproduct/product\u0026amp;product_id\u003d47", 
     "product_image": "http://ts.oc-develop.com/image/cache/catalog/demo/hp_1-500x500.jpg" 
    }, 
    { 
     "product_sku": "41", 
     "product_name": "iMac", 
     "product_description": "\r\nJust when you thought iMac had everything,", 
     "product_price": "100.0000", 
     "product_url": "http://ts.oc-develop.com/index.php?route\u003dproduct/product\u0026amp;product_id\u003d41", 
     "product_image": "http://ts.oc-develop.com/image/cache/catalog/demo/imac_1-500x500.jpg" 
    } 
    ] 
} 

現在只是想知道爲什麼他們逃脫了「/」,如HMAC,沒有提及是否在做這個api文檔,但是在json示例中顯然存在,是由json_encode還是別的,這對我來說似乎沒有必要。

轉義只是由他們複製並粘貼其日誌輸出引起的。既然這個問題已經得到了相當具體,我要去刪除它似乎是有用的給其他人

+4

使用Google搜索「java json encode」沒有爲您提供任何有用的信息嗎? – ceejayoz

+0

@ceejayoz但輸出必須是相同的,所以我正在尋找有此實際經驗的人,我剛剛與base64的simailr問題糾纏在一起,Java未返回與php base64_encode函數相同的值 –

+0

然後向我們展示一個您編寫的Java代碼示例,PHP預期的JSON結果以及不正確的Java JSON結果。如果你做對了,完全可以生成JSON *。 – ceejayoz

回答

0

有一個在Java中沒有等價的,但你可以嘗試使用一些第三方的庫,例如GSON或org.json。*

+0

是的,我使用了gson,它工作得很好, 謝謝。 –

0

如果結果對象與您的示例一樣保持「平坦」狀態,您可以考慮自己構建對象,我也曾多次進行此操作以避免使用外部庫。只需循環遍歷數組並填充例如一個字符串。

+0

我剛剛意識到它的notflat,因爲$ value也是一個數組,Ill'update問題 –

+0

爲什麼downvote? –

+0

@GerardBlinkhorst對不起,我想我偶然點擊了它 –