Android Studio中如何编写指定格式的JSON对象?
在Android Studio中生成指定JSON对象的实现方法
嘿,作为论坛新手能直接明确需求很棒!在Android里生成你要的JSON对象,有两种常用方式,我给你分别写示例代码,你可以根据自己的项目情况选:
方法一:使用Android原生JSONObject API(无需额外依赖)
这种方法适合结构不复杂的JSON,直接用Android自带的org.json包下的类来构建,不需要引入第三方库,上手快。
import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public class ShipmentJsonGenerator { public static String createShipmentJson() { try { // 先构建最内层的parcel对象 JSONObject parcel = new JSONObject(); parcel.put("quantity", "1"); parcel.put("weight", "3"); parcel.put("weight_unit", "kg"); parcel.put("length", "10"); parcel.put("height", "20"); parcel.put("width", "30"); parcel.put("dimension_unit", "cm"); // 将parcel放入JSON数组(因为parcels是数组类型) JSONArray parcelsArray = new JSONArray(); parcelsArray.put(parcel); // 构建shipment对象 JSONObject shipment = new JSONObject(); shipment.put("shipment_type", "Package"); shipment.put("parcels", parcelsArray); // 构建origin_direction对象 JSONObject originDirection = new JSONObject(); originDirection.put("country_code", "MX"); originDirection.put("postal_code", "11550"); // 构建destination_direction对象(你提供的内容里邮编没写完,这里留个占位符) JSONObject destinationDirection = new JSONObject(); destinationDirection.put("country_code", "MX"); destinationDirection.put("postal_code", "YOUR_DEST_POSTAL_CODE"); // 替换为实际邮编 // 构建最外层的根JSON对象 JSONObject rootJson = new JSONObject(); rootJson.put("enviaya_account", "Y0DCRGIU"); rootJson.put("carrier_account", JSONObject.NULL); // 注意这里要用JSONObject.NULL,不能直接写Java的null rootJson.put("api_key", "YOUR_API_KEY"); rootJson.put("shipment", shipment); rootJson.put("origin_direction", originDirection); rootJson.put("destination_direction", destinationDirection); // 返回格式化后的JSON字符串(带缩进,方便查看) return rootJson.toString(4); } catch (JSONException e) { e.printStackTrace(); return null; } } }
⚠️ 注意:处理JSON中的null值时,必须用JSONObject.NULL,如果直接传Java的null会抛出异常哦。
方法二:使用Gson库(适合复杂结构/对象映射)
如果你的JSON结构比较复杂,或者需要频繁在Java对象和JSON之间转换,用Google的Gson库会更高效,代码也更简洁易维护。
第一步:添加Gson依赖
在Module级别的build.gradle的dependencies块中添加:
implementation 'com.google.code.gson:gson:2.10.1' // 可以替换为最新版本
第二步:创建对应Java实体类
每个JSON层级对应一个Java类,字段名要和JSON的key完全一致:
import java.util.List; // 最外层的请求对象 public class ShipmentRequest { public String enviaya_account; public Object carrier_account; // 用Object接收null值,直接赋值null即可 public String api_key; public Shipment shipment; public Direction origin_direction; public Direction destination_direction; // Shipment子对象 public static class Shipment { public String shipment_type; public List<Parcel> parcels; } // Parcel子对象 public static class Parcel { public String quantity; public String weight; public String weight_unit; public String length; public String height; public String width; public String dimension_unit; } // Direction子对象(origin和destination结构一致,复用这个类) public static class Direction { public String country_code; public String postal_code; } }
第三步:生成JSON字符串
import com.google.gson.Gson; import com.google.gson.GsonBuilder; import java.util.Collections; public class ShipmentJsonGeneratorWithGson { public static String createShipmentJson() { // 初始化Gson实例,开启格式化输出 Gson gson = new GsonBuilder().setPrettyPrinting().create(); // 逐层构建Java对象 ShipmentRequest request = new ShipmentRequest(); request.enviaya_account = "Y0DCRGIU"; request.carrier_account = null; // 直接赋值null,Gson会自动转成JSON的null request.api_key = "YOUR_API_KEY"; ShipmentRequest.Shipment shipment = new ShipmentRequest.Shipment(); shipment.shipment_type = "Package"; ShipmentRequest.Parcel parcel = new ShipmentRequest.Parcel(); parcel.quantity = "1"; parcel.weight = "3"; parcel.weight_unit = "kg"; parcel.length = "10"; parcel.height = "20"; parcel.width = "30"; parcel.dimension_unit = "cm"; shipment.parcels = Collections.singletonList(parcel); // 把单个parcel包装成列表 request.shipment = shipment; ShipmentRequest.Direction origin = new ShipmentRequest.Direction(); origin.country_code = "MX"; origin.postal_code = "11550"; request.origin_direction = origin; ShipmentRequest.Direction destination = new ShipmentRequest.Direction(); destination.country_code = "MX"; destination.postal_code = "YOUR_DEST_POSTAL_CODE"; // 替换为实际邮编 request.destination_direction = destination; // 将Java对象转成JSON字符串 return gson.toJson(request); } }
两种方法都能生成你需要的JSON格式,原生方法不用加依赖,适合简单场景;Gson更适合复杂结构或者需要频繁序列化/反序列化的情况。如果还有疑问随时问!
内容的提问来源于stack exchange,提问作者HUMBERTO VALLE




