Single line output from JSON.stringify

问题: I am converting complex object using JSON stringify. I want this kind of output so I can bind it easily with my model. DateRequired: "2019-02-02" DeliveryDestination: "...

问题:

I am converting complex object using JSON stringify.

I want this kind of output so I can bind it easily with my model.

DateRequired: "2019-02-02"
DeliveryDestination: "test"
ProjectCode: "002P"
RequestItems: {InventoryItemsId: "2", Brand: "NIKE", Types: "Casual", Specification: "Mentality", ItemName: "Wild"},
{InventoryItemsId: "3", Brand: "PUMA", Types: "Running", Specification: "Energy", ItemName: "Wild"}

But instead I am getting this one.

DateRequired: "2019-02-02"
DeliveryDestination: "test"
ProjectCode: "002P"
RequestItems: Array(2)
0: {InventoryItemsId: "2", Brand: "NIKE", Types: "Casual", Specification: "Mentality", ItemName: "Wild", …}
1: {InventoryItemsId: "3", Brand: "PUMA", Types: "Running", Specification: "Energy", ItemName: "Wild", …}
length: 2
__proto__: Array(0)
__proto__: Object

This is the code that post the data:

var items = postAllItems(); //this is an array
                var materialRequest = {
                    'DateRequired': $('#txtDateRequired').val(),
                    'ProjectCode': $('#txtProjectCode').val(),
                    'DeliveryDestination': $('#txtDeliveryDestination').val(),
                    'RequestItems': items
            };

 postMaterialRequest(materialRequest);

function postMaterialRequest(materials) {
            $.ajax({
                contentType: 'application/json',
                type: 'POST',
                url: '/api/PostMaterial',
                data: JSON.stringify(materials),
                success: function (data) {
                    console.log("Info Save " + data);                   
                },
                failure: function (response) {
                    console.log('Failed to save.');
                }
            });
        }

This is what I am getting from Web API

enter image description here


回答1:

It is the right kind of output - it's just the console's way of showing an object. Using StackOverflow's console, or using JSON.stringify, shows the output you want:

var myObj = {
  DateRequired: "2019-02-02",
  DeliveryDestination: "test",
  ProjectCode: "002P",
  RequestItems: [{
      InventoryItemsId: "2",
      Brand: "NIKE",
      Types: "Casual",
      Specification: "Mentality",
      ItemName: "Wild"
    },
    {
      InventoryItemsId: "3",
      Brand: "PUMA",
      Types: "Running",
      Specification: "Energy",
      ItemName: "Wild"
    }
  ]
};

console.log(myObj);
console.log(JSON.stringify(myObj));
.as-console-wrapper { max-height: 100% !important; top: 0; }

  • 发表于 2019-03-03 20:14
  • 阅读 ( 184 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除