1
var singleItems = new List<Products>();
singleItems.Add(new Products() { product_id = 1, title = "Bryon Hetrick", price = 50 });
singleItems.Add(new Products() { product_id = 2, title = "Nicole Wilcox", price = 20 });
var serializer = new JavaScriptSerializer();
var serializedResult = serializer.Serialize(serializer);
從上面的示例代碼中,我得到的Json輸出像波紋管。從JavaScriptSerializer輸出json格式
[{"product_id":1,"title":"Bryon Hetrick","price":50},
{"product_id":2,"title":"Nicole Wilcox","price":20}]
但我的Json需要一個更多的值 - 「配置」,我也需要格式化完全像波紋管的整個數據。如何編輯我的C#代碼來實現該值?
{ "products":[{"product_id":"B071H6TBM5","title":"New Iphone 5S","price":"23.45"},{"product_id":"B071DM968J","title":"Iphone 4 old","price":"23.45"}],"config":{"token":"","Site":"Us","Mode":"ListMyItem"}}
有沒有辦法做到這一點而不使其模型類? –
@Johnpoe - 序列化程序查看您提供的對象並將其轉換爲JSON,因此您必須修改您的RegisteredUsers類以包含輸出的Config屬性。這可能會打破你的設計,所以我會建議有一箇中間班,如我的答案中所示。 –
我編輯了這個問題。 –