Javascript 更改对象上的字符串

Javascript 更改对象上的字符串,javascript,json,Javascript,Json,我有从RESTAPI得到的字符串 “xfields”:"(12)本月月月日日(12)月月月月日(12)月日(12)月月月日(12)日日(12)日)bibibihd当天(2008/17/1976年年(12)月月月日(17)年年(1976年)1976年(1976年)年年年年年(17)年)年(17年)年(17年)年)年(17年)年(17年)年)年(17年)年)年(17年)年(17年)年)年(17年)年(17月)年)年)年(17年)年)年(17月月月月月月月月(17日)日)年(1976年)日)年(17日

我有从RESTAPI得到的字符串

“xfields”:"(12)本月月月日日(12)月月月月日(12)月日(12)月月月日(12)日日(12)日)bibibihd当天(2008/17/1976年年(12)月月月日(17)年年(1976年)1976年(1976年)年年年年年(17)年)年(17年)年(17年)年)年(17年)年(17年)年)年(17年)年)年(17年)年(17年)年)年(17年)年(17月)年)年)年(17年)年)年(17月月月月月月月月(17日)日)年(1976年)日)年(17日)年)年(17月月月(1976年)年)年(1976年)日)年)年(1976年)年(17月月月月(17日)日)日)月(1976年)年)年)年(1976年)年(1976年)年)| 6.1 |体重| 74 |体重|磅| 163 | | |胸围| 102 | | | |胸围| 40 | |胸围|腰围|腰围| 27 | | |臀围| 98 |臀围| 38 | | | | | | |鞋号| 7 | | | | | | | img | 2018-08/1533214206 | shar-jackson-height-weight-body-measurement.jpg“

我确实需要将这个字符串改为object-like

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

我怎么做?我知道,我应该使用function.map()和.split(),但我不知道如何做得更好。

你可以通过将原始字符串拆分为
|
来获得对,然后使用
数组#reduce
对其进行迭代

在每次迭代中,按
|
再次拆分该对,并将键/值指定给结果对象

const字符串=”(12)本月月月日日(12)月月月月日(12)月日(12)月月月日(12)日日(12)日)bibibihd当天(2008/17/1976年年(12)月月月日(17)年年(1976年)1976年(1976年)年年年年年(17)年)年(17年)年(17年)年)年(17年)年(17年)年)年(17年)年)年(17年)年(17年)年)年(17年)年(17月)年)年)年(17年)年)年(17月月月月月月月月(17日)日)年(1976年)日)年(17日)年)年(17月月月(1976年)年)年(1976年)日)年)年(1976年)年(17月月月月(17日)日)日)月(1976年)年)年)年(1976年)年(1976年)年)| 6.1 |体重| 74 |体重|磅| 163 | | |胸围| 102 | | | |胸围| 40 | |胸围|腰围|腰围| 27 | | |臀围| 98 |臀围| 38 | | | | | | |鞋号| 7 | | | | | | | | img | 2018-08/1533214206 | shar-jackson-height-weight-body-measurement.jpg“;
const obj=string.split(“| |”)
.减少((a,成对)=>{
让[key,value]=pairs.split(“|”);
a[键]=值;
返回a;
}, {});

console.log(obj);
您可以尝试使用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);
    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);
或者您也可以使用
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('||')
             .reduce((acc, pairs) => {
                let [key, value] = pairs.split('|');
                acc[key] = value;
                return acc;
             }, {});
console.log(JSONobj);
    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);

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

const str=`“xfields”:(12)本月月月日日(12)月月月月日(12)月日(12)月月月日(12)日日(12)日)bibibihd当天(2008/17/1976年年(12)月月月日(17)年年(1976年)1976年(1976年)年年年年年(17)年)年(17年)年(17年)年)年(17年)年(17年)年)年(17年)年)年(17年)年(17年)年)年(17年)年(17月)年)年)年(17年)年)年(17月月月月月月月月(17日)日)年(1976年)日)年(17日)年)年(17月月月(1976年)年)年(1976年)日)年)年(1976年)年(17月月月月(17日)日)日)月(1976年)年)年)年(1976年)年(1976年)年)| 6.1 |体重| 74 |体重|磅| 163 | | |胸围| 102 | | | |胸围| 40 | |胸围|腰围|腰围| 27 | | |臀围| 98 |臀围| 38 | | | | | | |鞋号| 7 | | | | | | | | img | 2018-08/1533214206 | shar-jackson-height-weight-body-measurement.jpg`;
const obj=JSON.parse(`{${str}}`);
obj.xfields=Object.assign(…obj.xfields.split(“| |”)
.map(s=>{
常量[key,value]=s.split(“|”);
返回{[key]:value};
}));
console.log(obj);
您可以使用正则表达式

const myString=”(12)本月月月日日(12)月月月月日(12)月日(12)月月月日(12)日日(12)日)bibibihd当天(2008/17/1976年年(12)月月月日(17)年年(1976年)1976年(1976年)年年年年年(17)年)年(17年)年(17年)年)年(17年)年(17年)年)年(17年)年)年(17年)年(17年)年)年(17年)年(17月)年)年)年(17年)年)年(17月月月月月月月月(17日)日)年(1976年)日)年(17日)年)年(17月月月(1976年)年)年(1976年)日)年)年(1976年)年(17月月月月(17日)日)日)月(1976年)年)年)年(1976年)年(1976年)年)| 6.1 |体重| 74 |体重|磅| 163 | | |胸围| 102 | | | |胸围| 40 | |胸围|腰围|腰围| 27 | | |臀围| 98 |臀围| 38 | | | | | | |鞋号| 7 | | | | | | | | img | 2018-08/1533214206 | shar-jackson-height-weight-body-measurement.jpg“;
常量myRegexp=/([^\\\\]*)\\\.([^\\\\\\]*)(?:\\\\\\\\\\\$)/g;
match=myRegexp.exec(myString);
让结果={};
while(匹配!=null){
结果[匹配[1]]=匹配[2];
match=myRegexp.exec(myString);
}

console.log(result)
|
拆分以获得一个项目数组,然后按
|
拆分每个项目以获得键/值对。您是否尝试过解决此问题?您需要使用哪个浏览器?