2016-06-28 67 views
0

我有一個定義JSON結構的頂點代碼。我想就如何使用Apex將值分配給JSON上的字符串字段尋求建議。的JSON將具有陣列(PackageData),其包含應該包含值Json/Apex:將值分配給Json數組上的節點

字段

的Apex代碼:

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

回答

1

JSON總是串,所以有是解析器,它解析對象到JSON字符串,反之亦然

解析器做它的工作,並自動解析到字符串,你不用擔心該

JSON來對象:

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

對象到JSON:

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