有关json数组的问题

时间:2015-11-03 14:32:11

标签: c# json

我有一个JSON数组,我正在添加项目。我想以特定的格式显示这个JSON。

我的代码:

var array = new List<object>();
array.Add(new
        {
        Dealname = dealname,
        Ticketcount = tictnum,
        OriginalPrice = origpri,
        Dealsticketcount = dealsticktnu,
        dealprice = dp,
        totalprice = totamnt,
        });

   array.Add(new
       {
      ItemName = itnme,
      Price = price,
      Quantity = quant,
      });

这就是我的数组的样子。我正在添加一些项目。现在它产生以下输出:

[{"Dealname":"unnideal","Ticketcount":"25","OriginalPrice":"100","Dealsticketcount":"1","dealprice":"200","totalprice":"300},{"ItemName":"popcorn","Price":"100","Quantity":"1"},{"ItemName":"piza","Price":"100","Quantity":"1"}]

但我需要这样的输出:

[{"Dealname":"unnideal","Ticketcount":"25","OriginalPrice":"100","Dealsticketcount":"1","dealprice":"200","totalprice":"300"},"Offers"[{"ItemName":"popcorn","Price":"100","Quantity":"1"},{"ItemName":"piza","Price":"100","Quantity":"1"}]]

也就是说,我需要一个数组来提供优惠。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:0)

当商品需要成为主要对象的一部分时,您的问题似乎是您的父对象和子“商品”对象无关。

尝试这样的事情:

$remoteImage = "http://www.example.com/gifs/logo.gif";
$imginfo = getimagesize($remoteImage);
header("Content-type: $imginfo['mime']");
readfile($remoteImage);

答案 1 :(得分:0)

听起来你只想要另一个名为“优惠”的房产?

var array = new List<object>();

var offers = new[]
{
    new {ItemName = itnme, Price = price, Quantity = quant}
    ...
};

array.Add(new
    {
        Dealname = dealname,
        Ticketcount = tictnum,
        OriginalPrice = origpri,
        Dealsticketcount = dealsticktnu,
        dealprice = dp,
        totalprice = totamnt,
        Offers = offers  // adding Offers as a property here
    });

这应该产生如下的JSON:

[
  {
    "Dealname": "unnideal",
    "Ticketcount": "25",
    "OriginalPrice": "100",
    "Dealsticketcount": "1",
    "dealprice": "200",
    "totalprice": "300",
    "Offers": [
      {
        "ItemName": "popcorn",
        "Price": "100",
        "Quantity": "1"
      },
      {
        "ItemName": "piza",
        "Price": "100",
        "Quantity": "1"
      }
    ]
  }
]