【问题标题】:How i can convert string to json?我如何将字符串转换为 json?
【发布时间】:2020-12-10 22:01:00
【问题描述】:

'"{\"result\":{\"additionalDetails\":\"这里有更多详细信息。\",\"addressLine1\":\"123 Test St\",\"addressLine2\":null ,\"addressLine3\":null,\"addressLine4\":null,\"bedrooms\":3,\"cancelled\":null,\"competitingCompanies\":0,\"county\":\"测试County\",\"createdAt\":\"2020-08-20 15:57:44\",\"customerName\":\"Test Customer\",\"email\":\"test@email. com\",\"houseType\":\"分离式\",\"电话\":\"00000111111\",\"邮编\":\"CF11 9HB\",\"价格\":233000,\ "propertyType\":\"house\",\"quoteId\":\"Tes-1597935464\",\"surveyType\":\"Building\",\"town\":\"Test Town\"} ,\"timestamp\":1597935464,\"token\":\"c48753bc28e1b53b05eac8164356eeef2ee4ada1aa47df69ed\",\"signature\":\"e213b10fff881becdc41cd81f4275385034ac60f7dbf2c1d7

【问题讨论】:

  • JSON.parse()。请注意,您需要为该函数提供有效的 JSON,但您的示例不是 - 您需要删除 \\ 字符
  • 你能不能试着写一个问题,添加你有什么,你想要什么,看看tour学习How to Ask

标签: javascript jquery node.js reactjs


【解决方案1】:

我认为你可以使用 JSON.parse() 函数

const text = "{\"result\":{\"additionalDetails\":\"Additional details go here.\",\"addressLine1\":\"123 Test St\",\"addressLine2\":null,\"addressLine3\":null,\"addressLine4\":null,\"bedrooms\":3,\"cancelled\":null,\"competingCompanies\":0,\"county\":\"Test County\",\"createdAt\":\"2020-08-20 15:57:44\",\"customerName\":\"Test Customer\",\"email\":\"test@email.com\",\"houseType\":\"detached\",\"phone\":\"00000111111\",\"postcode\":\"CF11 9HB\",\"price\":233000,\"propertyType\":\"house\",\"quoteId\":\"Tes-1597935464\",\"surveyType\":\"Building\",\"town\":\"Test Town\"},\"timestamp\":1597935464,\"token\":\"c48753bc28e1b53b05eac8164356eeef2ee4ada1aa47df69ed\",\"signature\":\"e213b10fff881becdc41cd81f4275385034ac60f7dbf2c1d370c937ab7b29ae2\"}"

const json =JSON.parse(text)
console.log(json)
// {
//   "result": {
//     "additionalDetails": "Additional details go here.",
//     "addressLine1": "123 Test St",
//     "addressLine2": null,
//     "addressLine3": null,
//     "addressLine4": null,
//     "bedrooms": 3,
//     "cancelled": null,
//     "competingCompanies": 0,
//     "county": "Test County",
//     "createdAt": "2020-08-20 15:57:44",
//     "customerName": "Test Customer",
//     "email": "test@email.com",
//     "houseType": "detached",
//     "phone": "00000111111",
//     "postcode": "CF11 9HB",
//     "price": 233000,
//     "propertyType": "house",
//     "quoteId": "Tes-1597935464",
//     "surveyType": "Building",
//     "town": "Test Town"
//   },
//   "timestamp": 1597935464,
//   "token": "c48753bc28e1b53b05eac8164356eeef2ee4ada1aa47df69ed",
//   "signature": "e213b10fff881becdc41cd81f4275385034ac60f7dbf2c1d370c937ab7b29ae2"
// }

【讨论】:

  • 在问题示例中,您在字符串末尾放置了“,”。因此,如果删除那个“,”,则“{}”中的字符串格式正确。我尝试了那个字符串,我得到了一个有效的结果。如果你能解释你得到的错误。请
  • 如果你因为某种原因得到一个字符串并且想要删除你可以做 string.replace(/"/g ,"") 但我试图得到相同的行为,我不能
【解决方案2】:

好吧,显然,在将字符串解析为 JSON 之前,您需要在字符串中进行一些预替换

const str = '"{\"result\":{\"additionalDetails\":\"Additional details go here.\",\"addressLine1\":\"123 Test St\",\"addressLine2\":null,\"addressLine3\":null,\"addressLine4\":null,\"bedrooms\":3,\"cancelled\":null,\"competingCompanies\":0,\"county\":\"Test County\",\"createdAt\":\"2020-08-20 15:57:44\",\"customerName\":\"Test Customer\",\"email\":\"test@email.com\",\"houseType\":\"detached\",\"phone\":\"00000111111\",\"postcode\":\"CF11 9HB\",\"price\":233000,\"propertyType\":\"house\",\"quoteId\":\"Tes-1597935464\",\"surveyType\":\"Building\",\"town\":\"Test Town\"},\"timestamp\":1597935464,\"token\":\"c48753bc28e1b53b05eac8164356eeef2ee4ada1aa47df69ed\",\"signature\":\"e213b10fff881becdc41cd81f4275385034ac60f7dbf2c1d370c937ab7b29ae2\"}"';

// First trim " symbols at the beginning
// and at the end then parse as JSON
const res = JSON.parse(str.replace(/^\"(.*)\"$/, '$1'));

// Log
console.log(res);

【讨论】:

  • @Veera 这就是你需要的吗?
猜你喜欢
  • 2016-02-04
  • 2011-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-04
  • 1970-01-01
相关资源
最近更新 更多