Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
See more:
Hi,

I'm getting the below json format while converting the xml to json...


"Packages":

{
"PackageOccurrence": 1,
"PackagingId": "",
"Weight": 0.75,
"Length": 15,
"Width": 15,
"Height": 15
},
"Items": 


{
"ItemId": "",
"Quantity": 1,
"Description": "White Tee-shirt",
"Value": 100,
"Weight": 0.75,
"PackageOccurrence": 1,
"HsCode": "652534",
"SkuCode": "SKU3455692",
"CountryOfOrigin": "CN",
}


using the below code for convertion
string jsonText = JsonConvert.SerializeXmlNode(doc);
           JObject json = JObject.Parse(jsonText);



But my expected outcome should be as :

"Packages": 

[

{
"PackageOccurrence": 1,
"PackagingId": "",
"Weight": 0.75,
"Length": 15,
"Width": 15,
"Height": 15
}
]

"Items": 

[

{
"ItemId": "",
"Quantity": 1,
"Description": "White Tee-shirt",
"Value": 100,
"Weight": 0.75,
"PackageOccurrence": 1,
"HsCode": "652534",
"SkuCode": "SKU3455692",
"CountryOfOrigin": "CN",
"ImageUrl": "http://www.myimagestore.com/myimage.jpg"
}
]


is there any solution available for converting json property as array ?

What I have tried:

JObject Shipper = (JObject)rss["Shipment"];
Shipper.Property("Packages").ToArray();
Shipper.Property("Items").ToArray();
jsonText = rss.ToString();


but its not working for me
Posted
Updated 17-Mar-20 20:09pm
Comments
Richard Deeming 4-Mar-20 10:19am    
What does the XML node you're serializing look like?

1 solution

Step 1: You have to create the two class that represent your file.
Step 2: De-Serialize the Json file to these classes
Step 3: Change the values of that you want to change
Step 4: Serialize the classes as Json File finally.


The classes you can use are;

public class Items
   {
       public string ItemId { get; set; }
       public int Quantity { get; set; }
       public string Description { get; set; }
       public int Value { get; set; }
       public float Weight { get; set; }
       public int PackageOccurrence { get; set; }
       public string HsCode { get; set; }
       public string SkuCode { get; set; }
       public string CountryOfOrigin { get; set; }
   }

   public class Packages
   {
       public int PackageOccurrence { get; set; }
       public string PackagingId { get; set; }
       public float Weight { get; set; }
       public int Length { get; set; }
       public int Width { get; set; }
       public int Height { get; set; }
   }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900