簡體   English   中英

使用 java 將 Map 轉換為 Raw JSON 轉換

[英]Convert Map to Raw JSON convertion using java

["

{"id":4700248899822,"admin_graphql_api_id":"gid:\/\/shopify\/Order\/4700248899822","app_id":1758145,"browser_ip":null,"buyer_accepts_marketing":false,"cancel_reason":null,"cancelled_at":null,"cart_token":null,"checkout_id":null,"checkout_token":null,"closed_at":null,"confirmed":true,"contact_email":"marcpids@gmail.com","created_at":"2022-03-18T22:44:54+00:00","currency":"GBP","current_subtotal_price":"4924.99","current_subtotal_price_set":{"shop_money":{"amount":"4924.99","currency_code":"GBP"},"presentment_money":{"amount":"4924.99","currency_code":"GBP"}},"current_total_discounts":"0.00","current_total_discounts_set":{"shop_money":{"amount":"0.00","currency_code":"GBP"},"presentment_money":{"amount":"0.00","currency_code":"GBP"}},"current_total_duties_set":null,"current_total_price":"4929.98","current_total_price_set":{"shop_money":{"amount":"4929.98","currency_code":"GBP"},"presentment_money":{"amount":"4929.98","currency_code":"GBP"}},"current_total_tax":"0.00","current_total_tax_set":{"shop_money":{"amount":"0.00","currency_code":"GBP"},"presentment_money":{"amount":"0.00","currency_code":"GBP"}},"customer_locale":null,"device_id":null,"discount_codes":[],"email":"marcpids@gmail.com","estimated_taxes":false,"financial_status":"paid","fulfillment_status":null,"gateway":"manual","landing_site":null,"landing_site_ref":null,"location_id":null,"name":"27776","note":null,"note_attributes":[],"number":19545,"order_number":20545,"order_status_url":"https:\/\/www.shop.co.uk\/60948381934\/orders\/fa7e0285eed82f571ad456ded44f697e\/authenticate?key=dcff94c973659a1be74fb3d0f59424fd","original_total_duties_set":null,"payment_gateway_names":["manual"],"phone":null,"presentment_currency":"GBP","processed_at":"2022-03-18T22:44:54+00:00","processing_method":"","reference":null,"referring_site":null,"source_identifier":null,"source_name":"Matrixify App","source_url":null,"subtotal_price":"4924.99","subtotal_price_set":{"shop_money":{"amount":"4924.99","currency_code":"GBP"},"presentment_money":{"amount":"4924.99","currency_code":"GBP"}},"tags":"payment:fully-paid","tax_lines":[],"taxes_included":false,"test":false,"token":"fa7e0285eed82f571ad456ded44f697e","total_discounts":"0.00","total_discounts_set":{"shop_money":{"amount":"0.00","currency_code":"GBP"},"presentment_money":{"amount":"0.00","currency_code":"GBP"}},"total_line_items_price":"4924.99","total_line_items_price_set":{"shop_money":{"amount":"4924.99","currency_code":"GBP"},"presentment_money":{"amount":"4924.99","currency_code":"GBP"}},"total_outstanding":"0.00","total_price":"4929.98","total_price_set":{"shop_money":{"amount":"4929.98","currency_code":"GBP"},"presentment_money":{"amount":"4929.98","currency_code":"GBP"}},"total_price_usd":"6487.16","total_shipping_price_set":{"shop_money":{"amount":"4.99","currency_code":"GBP"},"presentment_money":{"amount":"4.99","currency_code":"GBP"}},"total_tax":"0.00","total_tax_set":{"shop_money":{"amount":"0.00","currency_code":"GBP"},"presentment_money":{"amount":"0.00","currency_code":"GBP"}},"total_tip_received":"0.00","total_weight":0,"updated_at":"2022-07-12T10:41:17+01:00","user_id":null,"billing_address":{"first_name":"Mark","address1":"Captains Row","phone":"07989408757","city":"Lymington","zip":"SO41 9RR","province":"England","country":"United Kingdom","last_name":"Pidsley","address2":null,"company":null,"latitude":50.7584704,"longitude":-1.537493,"name":"Mark Pidsley","country_code":"GB","province_code":"ENG"},"customer":{"id":6102973153518,"email":"marcpids@gmail.com","accepts_marketing":false,"created_at":"2022-03-18T03:18:48+00:00","updated_at":"2022-07-12T10:41:17+01:00","first_name":"Mark","last_name":"Pidsley","orders_count":3,"state":"invited","total_spent":"9896.94","last_order_id":4700248899822,"note":null,"verified_email":true,"multipass_identifier":null,"tax_exempt":false,"phone":null,"tags":"first-order:fully-paid, Guest","last_order_name":"27776","currency":"GBP","accepts_marketing_updated_at":"2022-03-18T03:18:48+00:00","marketing_opt_in_level":null,"tax_exemptions":[],"sms_marketing_consent":null,"admin_graphql_api_id":"gid:\/\/shopify\/Customer\/6102973153518","default_address":{"id":7602051678446,"customer_id":6102973153518,"first_name":"Mark","last_name":"Pidsley","company":"","address1":"Captains Row","address2":"","city":"Lymington","province":"England","country":"United Kingdom","zip":"SO41 9RR","phone":"07989408757","name":"Mark Pidsley","province_code":"ENG","country_code":"GB","country_name":"United Kingdom","default":true}},"discount_applications":[],"fulfillments":[],"line_items":[{"id":12045659570414,"admin_graphql_api_id":"gid:\/\/shopify\/LineItem\/12045659570414","fulfillable_quantity":1,"fulfillment_service":"manual","fulfillment_status":null,"gift_card":false,"grams":0,"name":"Cube Stereo Hybrid 160 HPC SL 2022 Bosch - XL 55cm","price":"4899.00","price_set":{"shop_money":{"amount":"4899.00","currency_code":"GBP"},"presentment_money":{"amount":"4899.00","currency_code":"GBP"}},"product_exists":true,"product_id":7570044977390,"properties":[{"name":"Pre-Order","value":""},{"name":"Frame Size (Check stock below)","value":"55cm"}],"quantity":1,"requires_shipping":true,"sku":"53710322","taxable":false,"title":"Cube Stereo Hybrid 160 HPC SL 2022 Bosch","total_discount":"0.00","total_discount_set":{"shop_money":{"amount":"0.00","currency_code":"GBP"},"presentment_money":{"amount":"0.00","currency_code":"GBP"}},"variant_id":42482856460526,"variant_inventory_management":"shopify","variant_title":"XL 55cm","vendor":"","tax_lines":[],"duties":[],"discount_allocations":[]},{"id":12045659603182,"admin_graphql_api_id":"gid:\/\/shopify\/LineItem\/12045659603182","fulfillable_quantity":1,"fulfillment_service":"manual","fulfillment_status":null,"gift_card":false,"grams":0,"name":"KMC Bosch Gen 4 38t Chainring (9mm Offset)","price":"25.99","price_set":{"shop_money":{"amount":"25.99","currency_code":"GBP"},"presentment_money":{"amount":"25.99","currency_code":"GBP"}},"product_exists":true,"product_id":7569830576366,"properties":[],"quantity":1,"requires_shipping":true,"sku":"BFCB45238","taxable":false,"title":"KMC Bosch Gen 4 38t Chainring (9mm Offset)","total_discount":"0.00","total_discount_set":{"shop_money":{"amount":"0.00","currency_code":"GBP"},"presentment_money":{"amount":"0.00","currency_code":"GBP"}},"variant_id":42435842703598,"variant_inventory_management":"shopify","variant_title":null,"vendor":"","tax_lines":[],"duties":[],"discount_allocations":[]}],"payment_terms":null,"refunds":[],"shipping_address":{"first_name":"Mark","address1":"Captains Row","phone":"07989408757","city":"Lymington","zip":"SO41 9RR","province":"England","country":"United Kingdom","last_name":"Pidsley","address2":"","company":"","latitude":50.7584704,"longitude":-1.537493,"name":"Mark Pidsley","country_code":"GB","province_code":"ENG"},"shipping_lines":[{"id":3913660694766,"carrier_identifier":null,"code":"UK E\u0026amp;W Zone A (Weight: 24.75kg)","delivery_category":null,"discounted_price":"4.99","discounted_price_set":{"shop_money":{"amount":"4.99","currency_code":"GBP"},"presentment_money":{"amount":"4.99","currency_code":"GBP"}},"phone":null,"price":"4.99","price_set":{"shop_money":{"amount":"4.99","currency_code":"GBP"},"presentment_money":{"amount":"4.99","currency_code":"GBP"}},"requested_fulfillment_service_id":null,"source":"shopify","title":"UK E\u0026amp;W Zone A (Weight: 24.75kg)","tax_lines":[],"discount_allocations":[]}]}

將 Map 序列化為 JSON 在 Java 中是一項非常簡單的任務。 您可以使用任何 JSON 庫,最流行的是 Json Jackson(也稱為 Faster XML)或 Google 的 Gson。 但是,如果您想要更簡單的解決方案,我在 Jackson Json 上編寫了自己的瘦包裝器,它允許您將對象序列化為 JSON 或將其從 Json 反序列化為 Object。 對於您的情況,代碼可以像這樣簡單

Map<String, Object) map = ... //here is your map that you got from your source
String jsonString;

    try {
      jsonString = JsonUtils.writeObjectToJsonString(map);
    }catch(IOException ioe) {
      //Handle exception
    }

這是JsonUtils 類的 Javadoc 它是我編寫和維護的 MgntUtils 開源庫的一部分。 您可以在此處或從 Github 將庫作為 maven 工件獲取,包括源代碼和 Javadoc此處

Map<String, String> elements = new HashMap();
elements.put("Key1", "Value1");
elements.put("Key2", "Value2");

ObjectMapper objectMapper = new ObjectMapper();

try {
    String json = objectMapper.writeValueAsString(elements);
    System.out.println(json);
} catch (JsonProcessingException e) {
    e.printStackTrace();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM