0

Please check from Json output i wanted using JavaScriptSerializer. Then check class helper i created using json2csharp.com. Problem is in controller. I am not getting correct output as per my required Jason. I am doing correct in controller? Where the problem can be? Please ask question if you want to know something specific, sorry its hard to describe more clearly.

Helper Class Code:

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; }
    }

Controller code:

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);
         }

Required Json Output code:

{
  "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
  • I don't see anywhere that you're setting the File property on the root object... It also appears that you haven't assigned the details to the File object, nor the ItemsFromFile to the detail object. Commented Aug 25, 2017 at 16:10
  • You may simply use the Json ,method. return Json(rootObject); Commented Aug 25, 2017 at 16:12

2 Answers 2

1

You can initialize internal objects in the code in the constructor as well.

   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; }
    }

Initialize your items properly. And create Root Object from the ground up. Populate the internal classes first and then later ones.

   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);
Sign up to request clarification or add additional context in comments.

Comments

1

Ensure that all your objects are assigned appropriately.

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);

That being said, it appears that you do not need the list of products inside of the ItemsFromFile class. This definition likely makes more sense:

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

Then your code would be something like this:

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 });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.