从解析的JSON字符串

时间:2015-10-20 13:33:56

标签: javascript jquery arrays json

我有一个解析过的Json字符串,我想进一步分成4个不同的数组。我尝试使用$.parseJson('[' + data + ']');将所有数据返回到一个数组中。更确切地说,我需要它看起来更像catData = [Object, Object, Object, Object]感谢您的帮助。

我的数据看起来像这样

  [{\"LkpMasterID\":491,\"LkpMasterCode\":\"CAT INSURANCE\",\"LkpMasterDescription\":\"25\",\"Attribute\":\"Cat Values\",\"Dependency\":null,\"LkpName\":\"CAT INSURANCE\",\"IsAttribute\":false,\"IsActive\":true,\"CreatedBy\":52834,\"CreatedOn\":\"2015-09-15T15:25:46.273\",\"UpdatedBy\":null,\"UpdatedOn\":null,\"IsDeleted\":false,\"ProcessedPage\":\"Master.aspx\",\"DisplayOrder\":1},{\"LkpMasterID\":492,\"LkpMasterCode\":\"CAT SUPPLEMENTS\",\"LkpMasterDescription\":\"50\",\"Attribute\":\"Cat Values\",\"Dependency\":null,\"LkpName\":\"CAT SUPPLEMENTS\",\"IsAttribute\":false,\"IsActive\":true,\"CreatedBy\":52834,\"CreatedOn\":\"2015-09-15T15:28:36.2\",\"UpdatedBy\":56366,\"UpdatedOn\":\"2015-09-16T10:26:36.95\",\"IsDeleted\":false,\"ProcessedPage\":\"Master.aspx\",\"DisplayOrder\":2},{\"LkpMasterID\":493,\"LkpMasterCode\":\"OTHER CATS\",\"LkpMasterDescription\":\"30\",\"Attribute\":\"Cat Values\",\"Dependency\":null,\"LkpName\":\"OTHER CATS\",\"IsAttribute\":false,\"IsActive\":true,\"CreatedBy\":56366,\"CreatedOn\":\"2015-09-16T10:27:37.777\",\"UpdatedBy\":null,\"UpdatedOn\":null,\"IsDeleted\":false,\"ProcessedPage\":\"Master.aspx\",\"DisplayOrder\":3},{\"LkpMasterID\":495,\"LkpMasterCode\":\"SHORT-TERM CATS\",\"LkpMasterDescription\":\"30\",\"Attribute\":\"CAT Values\",\"Dependency\":null,\"LkpName\":\"SHORT-TERM CATS\",\"IsAttribute\":false,\"IsActive\":true,\"CreatedBy\":56366,\"CreatedOn\":\"2015-10-02T00:00:00\",\"UpdatedBy\":null,\"UpdatedOn\":null,\"IsDeleted\":false,\"ProcessedPage\":\"Master.aspx\",\"DisplayOrder\":4}]"


$.getJson('LkpMasterTable', function (data) { var catData = JSON.Parse(data)) }   

var catData = [
 {"LkpMasterID":491,"LkpMasterCode":"CAT  INSURANCE","LkpMasterDescription":"25","Attribute":"Cat Values","Dependency":null,"LkpName":"CAT INSURANCE","IsAttribute":false,"IsActive":true,"CreatedBy":52834,"CreatedOn":"2015-09-15T15:25:46.273","UpdatedBy":null,"UpdatedOn":null,"IsDeleted":false,"ProcessedPage":"Master.aspx","DisplayOrder":1},

 {"LkpMasterID":492,"LkpMasterCode":"CAT SUPPLEMENTS","LkpMasterDescription":"50","Attribute":"Cat Values","Dependency":null,"LkpName":"CAT SUPPLEMENTS","IsAttribute":false,"IsActive":true,"CreatedBy":52834,"CreatedOn":"2015-09-15T15:28:36.2","UpdatedBy":56366,"UpdatedOn":"2015-09-16T10:26:36.95","IsDeleted":false,"ProcessedPage":"Master.aspx","DisplayOrder":2},

 {"LkpMasterID":493,"LkpMasterCode":"OTHER CATS","LkpMasterDescription":"30","Attribute":"Cat Values","Dependency":null,"LkpName":"OTHER CATS","IsAttribute":false,"IsActive":true,"CreatedBy":56366,"CreatedOn":"2015-09-16T10:27:37.777","UpdatedBy":null,"UpdatedOn":null,"IsDeleted":false,"ProcessedPage":"Master.aspx","DisplayOrder":3},

 {"LkpMasterID":495,"LkpMasterCode":"SHORT-TERM CATS","LkpMasterDescription":"30","Attribute":"Cat Values","Dependency":null,"LkpName":"SHORT-TERM CATS","IsAttribute":false,"IsActive":true,"CreatedBy":56366,"CreatedOn":"2015-10-02T00:00:00","UpdatedBy":null,"UpdatedOn":null,"IsDeleted":false,"ProcessedPage":"Master.aspx","DisplayOrder":4}
  ]

1 个答案:

答案 0 :(得分:3)

FIDDLE

你可以做的是使用jQuery' s map function来"将数组或对象中的所有项目翻译成新的项目数组。"

PHPUnit 4.7.7 by Sebastian Bergmann and contributors.

Test 'FooTest::testFoo              ' ended.    Total time 2.050 s. Test time 0.026 s. 
.Test 'FooTest::testBar             ' ended.    Total time 2.077 s. Test time 1.000 s.
.Test 'FooTest::testBar2            ' ended.    Total time 3.077 s. Test time 0.730 s.

使用地图的results看起来像' catData = [对象,对象,对象,对象]'。