[英]JS Convert a string into a json object
需要將字符串轉換為json對象。
json對象/字符串的規則:
String Example:
var str = "1863707152859270^Exercise to lose weight^289169006^Reduce 20 pounds^Walk daily for one hour. Have a light dinner.^5/10/2013 12:00:00 AM^^1/21/2019 4:25:52 PM^Y^Frank the Tank^1/22/2019 8:50:02 AM^1/22/2019 8:50:02 AM^Frank the Tank^Abnormal LGSIL^1848065703239670^1863707006859070|1863707222859280^Exercise to lose weight^289169006^Reduce 20 pounds^Walk daily for one hour. Have a light dinner.^5/10/2013 12:00:00 AM^^1/21/2019 4:27:02 PM^Y^Frank the Tank^1/22/2019 8:50:02 AM^1/22/2019 8:50:02 AM^Frank the Tank^Abnormal LGSIL^1848065703239670^1863707006859070"
嘗試:
var str = "1863707152859270^Exercise to lose weight^289169006^Reduce 20 pounds^Walk daily for one hour. Have a light dinner.^5/10/2013 12:00:00 AM^^1/21/2019 4:25:52 PM^Y^Frank the Tank^1/22/2019 8:50:02 AM^1/22/2019 8:50:02 AM^Frank the Tank^Abnormal LGSIL^1848065703239670^1863707006859070|1863707222859280^Exercise to lose weight^289169006^Reduce 20 pounds^Walk daily for one hour. Have a light dinner.^5/10/2013 12:00:00 AM^^1/21/2019 4:27:02 PM^Y^Frank the Tank^1/22/2019 8:50:02 AM^1/22/2019 8:50:02 AM^Frank the Tank^Abnormal LGSIL^1848065703239670^1863707006859070" function ParseDelimList(str){ var result=[]; var rows = str.split('|'); var tmpString3 = []; for(var i=0;i<rows.length;i++){ var fields = rows[i].split('^'); var tmpString2 = []; for(var j=0;j<fields.length;j++){ var subfields = fields[j].split('~'); var tmpString1 = []; for(var l=0;l<subfields.length;l++){ var tmp1 = "{"+l+":"+subfields[l]+"}," tmpString1.push(tmp1); }; var tmp2 = "{"+j+":"+tmpString1[j]+"},"; tmpString2.push(tmp2); }; var tmp3 = "{"+i+":"+tmpString2[i]+"},"; tmpString3.push(tmp3); }; return tmpString3; }; console.log(ParseDelimList(str))
最終結果(類似這樣):
var json = [
"1":{
{"0":"1863707152859270"},
{"1":"Exercise to lose weight"},
{"2":"289169006"},
{"3":"Reduce 20 pounds"},
{"4":"Walk daily for one hour. Have a light dinner."},
{"5":"5/10/2013 12:00:00 AM"},
{"6":"1/21/2019 4:25:52 PM"},
{"7":"Y"},
{"8":"Frank the Tank"},
{"9":"1/22/2019 8:50:02 AM"},
{"10":"1/22/2019 8:50:02 AM"},
{"11":"Frank the Tank"},
{"12":"Abnormal LGSIL"},
{"13":"1848065703239670"},
{"14":"1863707006859070"},
},
"2":{
{"0":"1863707152859270"},
{"1":"Exercise to lose weight"},
{"2":"289169006"},
{"3":"Reduce 20 pounds"},
{"4":"Walk daily for one hour. Have a light dinner."},
{"5":"5/10/2013 12:00:00 AM"},
{"6":"1/21/2019 4:25:52 PM"},
{"7":"Y"},
{"8":"Frank the Tank"},
{"9":"1/22/2019 8:50:02 AM"},
{"10":"1/22/2019 8:50:02 AM"},
{"11":"Frank the Tank"},
{"12":"Abnormal LGSIL"},
{"13":"1848065703239670"},
{"14":"1863707006859070"},
}
];
到這里,准備成為JSON.stringify
。 三重split
后再map
即可到達:
var str = "1863707152859270^Exercise to lose weight^289169006^Reduce 20 pounds^Walk daily for one hour. Have a light dinner.^5/10/2013 12:00:00 AM^^1/21/2019 4:25:52 PM^Y^Frank the Tank^1/22/2019 8:50:02 AM^1/22/2019 8:50:02 AM^Frank the Tank^Abnormal LGSIL^1848065703239670^1863707006859070|1863707222859280^Exercise to lose weight^289169006^Reduce 20 pounds^Walk daily for one hour. Have a light dinner.^5/10/2013 12:00:00 AM^^1/21/2019 4:27:02 PM^Y^Frank the Tank^1/22/2019 8:50:02 AM^1/22/2019 8:50:02 AM^Frank the Tank^Abnormal LGSIL^1848065703239670^1863707006859070"; var result = str.split('|').map(x => { return { row: x.split('^').map(y => { return { field: y.split('~').map(z => { return { subfield: z }; }) } }) } }); console.log(result);
由於您的預期結果並不完全清楚,如果有任何遺漏,請告訴我。
編輯 :添加了與下面的IE 11兼容的相同解決方案:
var str = "1863707152859270^Exercise to lose weight^289169006^Reduce 20 pounds^Walk daily for one hour. Have a light dinner.^5/10/2013 12:00:00 AM^^1/21/2019 4:25:52 PM^Y^Frank the Tank^1/22/2019 8:50:02 AM^1/22/2019 8:50:02 AM^Frank the Tank^Abnormal LGSIL^1848065703239670^1863707006859070|1863707222859280^Exercise to lose weight^289169006^Reduce 20 pounds^Walk daily for one hour. Have a light dinner.^5/10/2013 12:00:00 AM^^1/21/2019 4:27:02 PM^Y^Frank the Tank^1/22/2019 8:50:02 AM^1/22/2019 8:50:02 AM^Frank the Tank^Abnormal LGSIL^1848065703239670^1863707006859070"; var result = str.split('|').map(function(x) { return { row: x.split('^').map(function(y) { return { field: y.split('~').map(function(z) { return { subfield: z }; }) } }) } }); console.log(result);
建議:您實際上並不希望所有帶有key:value對的對象,而鍵只是索引。 如果僅具有數組(數組),則數據將更易於使用(並更好地反映源結構)。
var str = 'FOO^one|BAR|TEST^one^two^a$b$c^three';
var obj = str.split('|')
.map(s => s.split('^')
.map(x => x.split('$')));
console.log(JSON.stringify(obj));
您可以使用reduce split和map。
var str = "1863707152859270^Exercise to lose weight^289169006^Reduce 20 pounds^Walk daily for one hour. Have a light dinner.^5/10/2013 12:00:00 AM^^1/21/2019 4:25:52 PM^Y^Frank the Tank^1/22/2019 8:50:02 AM^1/22/2019 8:50:02 AM^Frank the Tank^Abnormal LGSIL^1848065703239670^1863707006859070|1863707222859280^Exercise to lose weight^289169006^Reduce 20 pounds^Walk daily for one hour. Have a light dinner.^5/10/2013 12:00:00 AM^^1/21/2019 4:27:02 PM^Y^Frank the Tank^1/22/2019 8:50:02 AM^1/22/2019 8:50:02 AM^Frank the Tank^Abnormal LGSIL^1848065703239670^1863707006859070" function ParseDelimList(str){ return str.split(/[|]+/).reduce((op,inp,index)=>{ op[index]= inp .split(/[\\^]+/) .map((e,i)=>({[i]:e})) //change this if needed return op },{}) }; console.log(ParseDelimList(str))
我在提供的輸入中看不到~
,因此未包含在答案中。
您只需在輸入中有~
更改地圖即可。
.map((e,i)=>({[i]:e.split(/[~]+/).map((e,i)=>({[i]:e})}))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.