Json/Apex:为 Json 数组上的节点赋值

Json/Apex: Assigning values to nodes on the Json Array

我有一个定义 JSON 结构的顶点代码。我想寻求有关如何使用 Apex 将值分配给 JSON 上的字符串字段的建议。 JSON 将有一个数组 (PackageData),其中包含应包含值

的字段

顶点代码:

public class Shipment{
    public PackageData[] PackageData;
}

public class PackageData{
    public Packaging Packaging;
    public Dimensions Dimensions;
    public PackageWeight PackageWeight;
}

public class Packaging{
    public string Code;
}

public class Dimensions{
    public UnitOfMeasurement UnitOfMeasurement;
    public string Length;
    public string Width;
    public string Height;
}

public class UnitOfMeasurement{
    public string Code;
}

public class PackageWeight{
    public UOM UOM;
    public string Weight;
}

public class UOM{
    public string Code;
}

JSON:

{
    "PackageData": [
        {
            "Packaging": {
                "Code": ""
            },
            "Dimensions": {
                "UnitOfMeasurement": {
                    "Code": ""
                },
                "Length": "",
                "Width": "",
                "Height": ""
            },
            "PackageWeight": {
                "UOM": {
                    "Code": ""
                },
                "Weight": ""
            }
        }   
    ]
}

JSON 总是字符串,所以有一个解析器将对象解析为 JSON String 和反之亦然

解析器完成它的工作并自动解析为 String,您不必为此担心

Json 对象:

ClassName objName = (ClassName) System.JSON.deserialize(jsonString, ClassName.class);

对象 Json:

String jsonString = System.JSON.serialize(objName);