在react native中将对象的字符串数组转换为json

cetgtptt  于 2023-01-14  发布在  React
关注(0)|答案(2)|浏览(229)

I have a string array of objects coming to a react native frontend. I'm not sure how to convert this to a json to read it in front end. This is the string I need to convert:
[{_id=63c1400ae18737cd3c67d8d4, user_id=1185.0, device_serial=544008293, site_id=2000000.0, datetime=2023-01-13T20:26:54.150Z, timezone=Asia/Seoul, code=11820, offset=3.24E7, __v=0.0, device_name=544008293, user_name=sanj, user_key=7722352345}, {_id=63c12467e18737cd3c67d809, user_id=1185.0, device_serial=544008293, site_id=2000000.0, datetime=2023-01-13T15:03:53.000Z, timezone=Asia/Seoul, code=10080, offset=3.24E7, __v=0.0, device_name=544008293, user_name=sanj, user_key=7722352345}]

thtygnil

thtygnil1#

let stringArr =
  '[{_id=63c1400ae18737cd3c67d8d4, user_id=1185.0, device_serial=544008293, site_id=2000000.0, datetime=2023-01-13T20:26:54.150Z, timezone=Asia/Seoul, code=11820, offset=3.24E7, __v=0.0, device_name=544008293, user_name=sanj, user_key=7722352345}, {_id=63c12467e18737cd3c67d809, user_id=1185.0, device_serial=544008293, site_id=2000000.0, datetime=2023-01-13T15:03:53.000Z, timezone=Asia/Seoul, code=10080, offset=3.24E7, __v=0.0, device_name=544008293, user_name=sanj, user_key=7722352345}]';
  
console.log("given array -> ",stringArr)

stringArr = stringArr.replace('[', '').replace(']', '').slice(1).slice(0, -1).split(',');

stringArr = stringArr.map(e => {
  const trimString = e.trim();
  const equalSpilt = trimString.split('=');
  const key = equalSpilt[0];
  const obj = {};
  obj[key] = equalSpilt[1];
  return obj;
});

console.log("final output -> ",stringArr)

如果可以将数组转换为字符串

let stringArr =
  '[{_id=63c1400ae18737cd3c67d8d4, user_id=1185.0, device_serial=544008293, site_id=2000000.0, datetime=2023-01-13T20:26:54.150Z, timezone=Asia/Seoul, code=11820, offset=3.24E7, __v=0.0, device_name=544008293, user_name=sanj, user_key=7722352345}, {_id=63c12467e18737cd3c67d809, user_id=1185.0, device_serial=544008293, site_id=2000000.0, datetime=2023-01-13T15:03:53.000Z, timezone=Asia/Seoul, code=10080, offset=3.24E7, __v=0.0, device_name=544008293, user_name=sanj, user_key=7722352345}]';

stringArr = stringArr.replace('[', '').replace(']', '').slice(1).slice(0, -1).split(',');

stringArr = stringArr.map(e => {
  const trimString = e.trim();
  const equalSpilt = trimString.split('=');
  const key = equalSpilt[0];
  const obj = {};
  obj[key] = equalSpilt[1];
  return obj;
});
g6baxovj

g6baxovj2#

您可以这样实现它:

let strval =
  "[{_id=63c1400ae18737cd3c67d8d4, user_id=1185.0, device_serial=544008293, site_id=2000000.0, datetime=2023-01-13T20:26:54.150Z, timezone=Asia/Seoul, code=11820, offset=3.24E7, __v=0.0, device_name=544008293, user_name=sanj, user_key=7722352345}, {_id=63c12467e18737cd3c67d809, user_id=1185.0, device_serial=544008293, site_id=2000000.0, datetime=2023-01-13T15:03:53.000Z, timezone=Asia/Seoul, code=10080, offset=3.24E7, __v=0.0, device_name=544008293, user_name=sanj, user_key=7722352345}]";

strval = strval.replace("[", "").replace("]", "");

const splittedObj = strval.split("},");

let arr = [];

splittedObj.forEach((e, i) => {
  e = e.replace("{", "").replace("}", "");
  let obj = {};

  e.split(",").map((element) => {
    const splittedElement = element.trim().split("=");
    obj[splittedElement[0]] = splittedElement[1];
  });
  arr.push(obj);
});

console.log(arr);

相关问题