更改对象上的字符串

时间:2018-08-02 13:24:57

标签: javascript json

我有从Rest Api得到的字符串。

  

“ xfields”:“年| 1976 ||月|八月||天| 31 || bithday | 1976年8月31日||年龄| 41 ||黄道十二宫|处女座||国籍|美国||职业|女演员||性取向|笔直|||眼色|棕色||染发|黑色||身高| 168 ||身高| 5 ||身高| 6.1 ||体重| 74 ||体重| 163 ||胸尺寸| 102 ||胸围| 40 ||胸围| 40D ||腰围| 70 ||腰围| 27 ||臀围| 98 ||臀围| 38 ||鞋围| 7 || monathdeath |一月|| img | 2018-08 / 1533214206_shar-jackson-height-weight-body-measurements.jpg“

我确实需要将此字符串更改为类似对象的对象。

"xfields": {
   "year": "1976",
   "month": "August"
   //...
}

我该怎么做?我知道,我应该使用.map()和.split()函数,但我不知道该怎么做。

4 个答案:

答案 0 :(得分:2)

您可以执行以下操作:将原始字符串除以||以得到配对,然后使用Array#reduce对其进行迭代。

在每次迭代中,用|再次将对分开,并将键/值分配给结果对象。

const string = "year|1976||month|August||day|31||bithday|08/31/1976||age|41||zodiac-sing|Virgo||nationality|USA||occupation|Actress||sexual-orientation|Straight||eyes_color|Brown||hair-color|Black||height|168||heightft|5||heightin|6.1||weight|74||weightlbs|163||chest-size|102||chestsizeft|40||bra-size|40D||waist-size|70||waistsizeft|27||hips-size|98||hipssizeft|38||shoes-size|7||monhtdeath|January||img|2018-08/1533214206_shar-jackson-height-weight-body-measurements.jpg";

const obj = string.split('||')
  .reduce((a, pairs) => {
    let [key, value] = pairs.split('|');
    a[key] = value;
    return a;
  }, {});

console.log(obj);

答案 1 :(得分:0)

您可以尝试使用 reduce 数组方法,如下所示

    let stringJson = `"xfields":"year|1976||month|August||day|31||bithday|08/31/1976||age|41||zodiac-sing|Virgo||nationality|USA||occupation|Actress||sexual-orientation|Straight||eyes_color|Brown||hair-color|Black||height|168||heightft|5||heightin|6.1||weight|74||weightlbs|163||chest-size|102||chestsizeft|40||bra-size|40D||waist-size|70||waistsizeft|27||hips-size|98||hipssizeft|38||shoes-size|7||monhtdeath|January||img|2018-08/1533214206_shar-jackson-height-weight-body-measurements.jpg"`;
stringJson = stringJson.split(":")[1].replace(/"/g, "");
const JSONobj = stringJson.split('||')
             .reduce((acc, pairs) => {
                let [key, value] = pairs.split('|');
                acc[key] = value;
                return acc;
             }, {});
console.log(JSONobj);

或者您也可以按以下方式使用forEach方法

    let stringJson = `"xfields":"year|1976||month|August||day|31||bithday|08/31/1976||age|41||zodiac-sing|Virgo||nationality|USA||occupation|Actress||sexual-orientation|Straight||eyes_color|Brown||hair-color|Black||height|168||heightft|5||heightin|6.1||weight|74||weightlbs|163||chest-size|102||chestsizeft|40||bra-size|40D||waist-size|70||waistsizeft|27||hips-size|98||hipssizeft|38||shoes-size|7||monhtdeath|January||img|2018-08/1533214206_shar-jackson-height-weight-body-measurements.jpg"`;
    stringJson = stringJson.split(":")[1].replace(/"/g, "");
    const JSONobj =  {};
    stringJson.split('||')
    .forEach((pairs, index) => {
        let [key, value] = pairs.split('|');
        JSONobj[key] = value;
    });
    console.log(JSONobj);

答案 2 :(得分:0)

您可以使用string.split()获取一个数组,然后使用array#map转换键值数组的数组,然后使用Object.assign创建一个对象。

const str = `"xfields":"year|1976||month|August||day|31||bithday|08/31/1976||age|41||zodiac-sing|Virgo||nationality|USA||occupation|Actress||sexual-orientation|Straight||eyes_color|Brown||hair-color|Black||height|168||heightft|5||heightin|6.1||weight|74||weightlbs|163||chest-size|102||chestsizeft|40||bra-size|40D||waist-size|70||waistsizeft|27||hips-size|98||hipssizeft|38||shoes-size|7||monhtdeath|January||img|2018-08/1533214206_shar-jackson-height-weight-body-measurements.jpg"`;
const obj = JSON.parse(`{${str}}`);
obj.xfields = Object.assign(...obj.xfields.split('||')
                 .map(s => { 
                    const [key,value] = s.split('|');
                    return {[key] : value};
                 }));
console.log(obj);

答案 3 :(得分:0)

您可以使用正则表达式

const myString = "year|1976||month|August||day|31||bithday|08/31/1976||age|41||zodiac-sing|Virgo||nationality|USA||occupation|Actress||sexual-orientation|Straight||eyes_color|Brown||hair-color|Black||height|168||heightft|5||heightin|6.1||weight|74||weightlbs|163||chest-size|102||chestsizeft|40||bra-size|40D||waist-size|70||waistsizeft|27||hips-size|98||hipssizeft|38||shoes-size|7||monhtdeath|January||img|2018-08/1533214206_shar-jackson-height-weight-body-measurements.jpg";

const myRegexp = /([^\|]*)\|([^\|]*)(?:\|\||$)/g;
match = myRegexp.exec(myString);
let result = {};
while (match != null) {
  result[match[1]] = match[2];
  match = myRegexp.exec(myString);
}
console.log(result)