从c#类中获取正确的json输出

时间:2017-08-25 16:06:34

标签: c# asp.net-mvc

请从我希望使用JavaScriptSerializer的Json输出中查看。然后检查我使用json2csharp.com创建的类助手。问题出在控制器中。根据我要求的Jason,我没有得到正确的输出。我在控制器中做得正确吗?问题出在哪里?如果你想知道具体的事情,请提问,对不起,很难更清楚地描述。

助手类代码:

public class ItemsFromFile
    {
        public string ASIN { get; set; }
        public string Title { get; set; }
        public List<Product> products { get; set; }

    }

    public class ItemsDeatails
    {
        public List<ItemsFromFile> ItemsFromFile { get; set; }
    }

    public class File
    {
        public string nameLocator { get; set; }
        public ItemsDeatails itemsDeatails { get; set; }
    }

    public class RootObject
    {
        public string Token { get; set; }
        public File file { get; set; }
    }

控制器代码:

if (type == "Salefreaks")
                {
            var token = ctx.BulkStores.FirstOrDefault(x => x.StoreName == store && x.Type == 1).Token;

            var ItemsFromFile = new ItemsFromFile()
                    {
                        products = new List<Product>()
                    };

                    var ItemsDeatails = new ItemsDeatails()
                    {

                    };

                    var File = new File()
                    {
                        nameLocator = "testimport1"
                    };

                    var RootObject = new RootObject()
                    {
                        Token = token
                    };

            var singleItems = ctx.BulkScannedItems.Where(x => x.UserSellerScanRequestId == id).ToList();

                    foreach (var item in singleItems)
                    {
                        ItemsFromFile.products.Add(new Product { ASIN = item.ASIN, Title = item.EbayTitle });
                    }

                    var json = new JavaScriptSerializer().Serialize(RootObject);
         }

必需的Json输出代码:

{
  "Token": "7f3099b0-36b1",
  "file": {
    "nameLocator": "testimport1",
     "itemsDeatails": {
      "ItemsFromFile": [
      {
         "ASIN": "B011KVFT9Y",
         "Title": "Disney Batman Durable Party Beach Outdoor Adventure Camp Chair w/ Storage Bag"
       },
       {
         "ASIN": "B01D4KRBW2",
         "Title": "High Quality Diy Oil Painting Paint Number Kit Theme-Romantic Street A Frameless"
       }
      ]
    }
  }
}

2 个答案:

答案 0 :(得分:1)

确保正确分配所有对象。

var token = ctx.BulkStores.FirstOrDefault(x => x.StoreName == store && x.Type == 1).Token;

var RootObject = new RootObject() {
    Token = token,
    file = new File() {
        nameLocator = "testimport1",
        itemsDeatails = new ItemsDeatails() {
            ItemsFromFile = new List<ItemsFromFile>()
        }
    }
};

var itemsFromFile = new ItemsFromFile();
itemsFromFile.products = new List<Product>();

var singleItems = ctx.BulkScannedItems.Where(x => x.UserSellerScanRequestId == id).ToList();

foreach (var item in singleItems) {
    itemsFromFile.products.Add(new Product { ASIN = item.ASIN, Title = item.EbayTitle });
}

RootObject.file.itemsDeatails.ItemsFromFile.Add(itemsFromFile);

var json = new JavaScriptSerializer().Serialize(RootObject);

话虽如此,您似乎不需要ItemsFromFile类中的产品列表。这个定义可能更有意义:

public class ItemsFromFile {
    public string ASIN { get; set; }
    public string Title { get; set; }
}

然后你的代码会是这样的:

var token = ctx.BulkStores.FirstOrDefault(x => x.StoreName == store && x.Type == 1).Token;

var RootObject = new RootObject() {
    Token = token,
    file = new File() {
        nameLocator = "testimport1",
        itemsDeatails = new ItemsDeatails() {
            ItemsFromFile = new List<ItemsFromFile>()
        }
    }
};

var singleItems = ctx.BulkScannedItems.Where(x => x.UserSellerScanRequestId == id).ToList();

foreach (var item in singleItems) {
    RootObject.file.itemsDeatails.ItemsFromFile.Add(new ItemsFromFile { ASIN = item.ASIN, Title = item.EbayTitle });
}

答案 1 :(得分:1)

您也可以在构造函数中的代码中初始化内部对象。

   public class RootObject
    {
        public string Token { get; set; }
        public File file { get; set; }
    }

    public class File
    {
        public File()
        {
            this.itemsDeatails = new ItemsDeatails();
        }
        public string nameLocator { get; set; }
        public ItemsDeatails itemsDeatails { get; set; }
    }

    public class ItemsDeatails
    {
        public ItemsDeatails(){
         this.ItemsFromFile = new List<ItemsFromFile>();
        }
        public List<ItemsFromFile> ItemsFromFile { get; set; }
    }

    public class ItemsFromFile
    {
        public ItemsFromFile(){
         this.products = new List<Product>();
        }

        public List<Product> products { get; set; }

    }

    public class Product {
        public string ASIN { get; set; }
        public string Title { get; set; }
    }

正确初始化您的商品。并从头开始创建Root对象。  先填充内部类,然后再填充以后的类。

   var itemDetails = new ItemsDeatails();

    itemDetails.ItemsFromFile = new ItemsFromFile();
    var singleItems = ctx.BulkScannedItems.Where(x => x.UserSellerScanRequestId == id).ToList();

    foreach (var item in singleItems)
            {
                itemDetails.ItemsFromFile.products.Add(new Product { ASIN = item.ASIN, Title = item.EbayTitle });
            }

    var fl = new File(){
        nameLocator = "testimport1",
        itemsDeatails = itemDetails
    }

    var token = ctx.BulkStores.FirstOrDefault(x => x.StoreName == store && x.Type == 1).Token;

    var root = new RootObject()
    {
        Token = token,
        file = fl
    }

    var json = new JavaScriptSerializer().Serialize(root);