简体   繁体   English

我如何将这个AJAX json结果(具有键/值属性)转换为javascript数组

[英]How can i convert this AJAX json result (which has a key/value property) into a javascript array

Given the following json result, how can I convert the validationErrors key/value property into a nice array object in javascript so i can then do things like 给定以下json结果,我该如何在Javascript中将validationErrors键/值属性转换为一个不错的数组对象,以便随后执行类似的操作

errors[0].Key

or 要么

errors[0].Value , etc... errors[0].Value等。

在此处输入图片说明

NOTE: If it's easier to do the conversion with jQuery, then I'm happy to use that. 注意:如果使用jQuery进行转换更容易,那么我很乐意使用它。 Also, I'm getting the data via jQuery -> $.post ...) 另外,我正在通过jQuery-> $.post ...获取数据。

update: 更新:

Here's the actual json data so someone can answer this with JSFiddle please. 这是实际的json数据,因此有人可以通过JSFiddle进行回答。

{
  "aaaa": 0,
  "bbbb": 0,
  "cccc": null,
  "validationErrors": {
    "a1_7127763-1c7ac823-61d5-483f-a9ca-4947e9eb8145": "Invalid PropertyType. Please choose any property except Unknown.",
    "a2_7127763-1c7ac823-61d5-483f-a9ca-4947e9eb8145": "A State is required. Eg. Victoria or New South Wales.",
    "b1_5433417-18b5568a-d18e-45e2-9c63-30796995e2d3": "Invalid PropertyType. Please choose any property except Unknown.",
    "b2_5433417-18b5568a-d18e-45e2-9c63-30796995e2d3": "A State is required. Eg. Victoria or New South Wales.",
    "c1_6655305-297c57f9-a460-4101-be7d-70c6b9a565d5": "Invalid PropertyType. Please choose any property except Unknown.",
    "c2_6655305-297c57f9-a460-4101-be7d-70c6b9a565d5": "A State is required. Eg. Victoria or New South Wales."
  }
}

I would take all the keys of the object and then map them to an array. 我将使用对象的所有键,然后将它们映射到数组。

var arrayOfErrors = Object.keys(objectOfErrors).map(function(errorKey) {
  return objectOfErrors[errorKey];
});

You can use map to convert the object to an array: 您可以使用map将对象转换为数组:

var errors = jQuery.map(data.validationErrors, function (value, key) {
    return {
        "Key": key,
        "Value": value
    };
});

JSFiddle showing this approach: http://jsfiddle.net/WgaFb/1/ JSFiddle展示了这种方法: http : //jsfiddle.net/WgaFb/1/


If you do not wish to use jQuery, here is a pure JavaScript method: 如果您不想使用jQuery,请使用以下纯JavaScript方法:

var errors = [];
for(var key in data.validationErrors) {
    errors.push({
        "Key": key,
        "Value": data.validationErrors[key]
    });
}

JSFiddle for this second approach: http://jsfiddle.net/4WXEF/1/ JSFiddle用于第二种方法: http : //jsfiddle.net/4WXEF/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用javascript在json中搜索指定的结果[键+值]? - How can i search for an specified result[key + value] in a json with javascript? 如何将 JSON 中的 SQL 主键转换为 javascript ZA8CFDE6331BD59EB266AC96F89111 和其他数据作为它的值 - How can I convert an SQL primary key in JSON to a javascript object key with the other data as it's value 如何在javascript中将json转换为数组? - How can i convert json to array in javascript? 如何将JSON转换为javascript数组? - How can I convert JSON into javascript array? 如何在JavaScript中将键值对对象数组转换为关联数组 - How can I convert an array of key value pair objects into an associative array in javascript 如何将用户名与数组中对象中的属性键匹配? - how can I match a username with the key of a property in an object which is in an array? 如何在JavaScript中将具有属性和键的对象转换为Array? - How to convert an object with property and key to Array in JavaScript? 如何使用easyXDM通过AJAX帖子将javascript对象/数组作为键值对发送? - How can I send an javascript object/array as key-value pairs via an AJAX post with easyXDM? 如何将字符串转换为键值json /数组? - How do I convert a string to a key value json / array? 如何将JSON数组转换为键值数组? - How to convert a JSON array to a key value array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM