简体   繁体   English

打字稿将数组转换为 JSON

[英]Typescript convert an array to JSON

I have a complicated data structure that I need to convert to JSON.我有一个复杂的数据结构,需要转换为 JSON。 The problem is that my field names and values are in an array.问题是我的字段名称和值都在一个数组中。

For instance, I have the following (simplified from my code base):例如,我有以下内容(从我的代码库中简化):

let SampleData = [
        { Field: 'Key', Value: '7'},
        { Field: 'City', Value: 'Some City'},
        { Field: 'Description', Value: 'Some Description'}
];

Basically my data is an array where the first element is the database column name, and the second element is the data in the column.基本上我的数据是一个数组,其中第一个元素是数据库列名,第二个元素是列中的数据。 I am trying to get a JSON object that is:我正在尝试获取一个 JSON 对象:

{ Key: 7, City: 'Some City', Description: 'Some Description' }

My real code has the fields and data is structures within the object, so I cannot simply use an Object.create() or Object.assign() as far as I can get working.我的真实代码具有对象中的字段和数据结构,因此我不能简单地使用 Object.create() 或 Object.assign() 就可以工作。

I have tried looping through to build a simple string and then use the JSON.parse to break it apart, but this seems like a lot of overhead for something I would have thought would be simpler.我曾尝试循环构建一个简单的字符串,然后使用 JSON.parse 将其分解,但这对于我认为会更简单的东西来说似乎是很多开销。

As you asked, here's how to do it:正如你所问,这是如何做到的:

  1. Mapping the array to an object将数组映射到对象
  2. Converting the object to JSON将对象转换为 JSON

 let array = [{ Field: 'Key', Value: '7' }, { Field: 'City', Value: 'Some City' }, { Field: 'Description', Value: 'Some Description' } ]; // #1 Mapping the array to an object... let obj = {}; array.forEach(item => obj[item.Field] = item.Value); // #2 Converting the object to JSON... let json = JSON.stringify(obj); console.log(json);

Bonus (ES6 + reduce):奖励(ES6 + 减少):

const obj = array.reduce((acc, { Field, Value }) => ({ ...acc, [Field]: Value }), {});

you can try the below approach .你可以试试下面的方法。 I have used spread operator(ES6) and Object.assign to create the object ,then converted it into json string.我使用了扩展运算符(ES6)和 Object.assign 来创建对象,然后将其转换为 json 字符串。

 let SampleData = [ { Field: 'Key', Value: '7'}, { Field: 'City', Value: 'Some City'}, { Field: 'Description', Value: 'Some Description'} ]; let obj = Object.assign(...SampleData.map( x => Object.values(x)).map(y => ({[y[0]]: y[1]}))); console.log(obj); //{ Key: "7", City: "Some City", Description: "Some Description" } console.log(JSON.stringify(obj));

I had a similar requirement and here is how I achieved it.我有一个类似的要求,这是我实现它的方法。

 var ranges: segmentRange[] = new Array(2); ranges[0] = { minimumPercentage: 50, maximumPercentage: 60 }; ranges[1] = { minimumPercentage: 30, maximumPercentage: 40 }; const segmentRanges = { segmentRanges: ranges }; return JSON.stringify(segmentRanges);

Output:输出:

{"segmentRanges":[{"minimumPercentage":50,"maximumPercentage":60},{"minimumPercentage":30,"maximumPercentage":40}]} {"segmentRanges":[{"minimumPercentage":50,"maximumPercentage":60},{"minimumPercentage":30,"maximumPercentage":40}]}

HTH,哈,

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM