JS将字符串转换为json对象

时间:2019-02-15 00:11:48

标签: javascript json

需要将字符串转换为json对象。


json对象/字符串的规则:

  1. 每次迭代加一。
  2. split('|'),这是json对象中的每一行。
  3. split('^'),这是行中的每个字段。
  4. split('〜'),这是该字段中的每个子字段。
  5. 需要考虑没有子字段,字段或行
  6. 需要在ES6之前的Internet Explorer 11中运行

  //final lowPrice = TextEditingController(); //after
  final lowPrice = MoneyMaskedTextController(decimalSeparator: '.', thousandSeparator: ','); //before

尝试:

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))

3 个答案:

答案 0 :(得分:3)

在这里,您准备好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);

答案 1 :(得分:2)

建议:您实际上并不需要所有具有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));

答案 2 :(得分:1)

您可以使用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})}))