简体   繁体   English

使用 java 将 Map 转换为 Raw JSON 转换

[英]Convert Map to Raw JSON convertion using java

["

I have enabled webhook on Shopify.<\/i>我在 Shopify 上启用了 webhook。<\/b> Shopify sends raw JSON in webhook response.<\/i> Shopify 在 webhook 响应中发送原始 JSON。<\/b> Somehow it is converted to map and now I want to convert it back to raw JSON that Shopify sends for HMAC verification.<\/i>不知何故,它被转换为地图,现在我想将其转换回 Shopify 发送用于 HMAC 验证的原始 JSON。<\/b> Can someone help how it can be done in JAVA?<\/i>有人可以帮助它如何在 JAVA 中完成吗?<\/b> What encoding is applied to data at Shopify end?<\/i> Shopify 端对数据应用什么编码?<\/b><\/p>

Shopify Webhook JSON:<\/i> Shopify Webhook JSON:<\/b><\/p>

{"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":[]}]}

Serializing Map to JSON is a very simple task in Java.将 Map 序列化为 JSON 在 Java 中是一项非常简单的任务。 You may use any of JSON libraries, the most popular are Json Jackson (also known asFaster XML) or Gson of Google.您可以使用任何 JSON 库,最流行的是 Json Jackson(也称为 Faster XML)或 Google 的 Gson。 But if you want even simpler solution, I wrote my own thin wrapper over Jackson Json that allows you to serialize an object to JSON or deserialize it from Json to Object.但是,如果您想要更简单的解决方案,我在 Jackson Json 上编写了自己的瘦包装器,它允许您将对象序列化为 JSON 或将其从 Json 反序列化为 Object。 For your case the code could be as simple as this对于您的情况,代码可以像这样简单

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
    }

Here is the Javadoc for JsonUtils class .这是JsonUtils 类的 Javadoc It is part of MgntUtils open source library written and maintained by me.它是我编写和维护的 MgntUtils 开源库的一部分。 You can get the library as maven artifact here or from Github, including source code and Javadoc here您可以在此处或从 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