繁体   English   中英

Javascript正则表达式从文本中获取json

[英]Javascript Regex to get json from a text

我需要从文本中获取json。 我在这里有一个示例,其中json在段落的下面,而我使用的正则表达式是({[\\w\\W\\s]+}) ,它将从大括号中获取包含在花括号中的文本,但来自我的示例文本它也从中间获得段落。 现在,我需要忽略这些段落,而只获取有效的json。

该示例来自Hubspot

Example POST URL:
https://api.hubapi.com/contacts/v1/contact/?hapikey=demo
Example POST body:
The code sample below represents some example JSON with 
standard fields to pass in the body of your request in 
order to create a new contact:
{
  "properties": [
    {
      "property": "email",
      "value": "testingapis@hubspot.com"
    },
    {
      "property": "firstname",
      "value": "Adrian"
    },
    {
      "property": "lastname",
      "value": "Mott"
    },
    {
      "property": "website",
      "value": "http://hubspot.com"
    },
    {
      "property": "company",
      "value": "HubSpot"
    },
    {
      "property": "phone",
      "value": "555-122-2323"
    },
    {
      "property": "address",
      "value": "25 First Street"
    },
    {
      "property": "city",
      "value": "Cambridge"
    },
    {
      "property": "state",
      "value": "MA"
    },
    {
      "property": "zip",
      "value": "02139"
    }
  ]
}

Returns an HTTP 200 response on success, with the response body containing the details of the new contact record:
{
  "identity-profiles": [
    {
      "identities": [
        {
          "timestamp": 1331075050646,
          "type": "EMAIL",
          "value": "fumanchu@hubspot.com"
        },
        {
          "timestamp": 1331075050681,
          "type": "LEAD_GUID",
          "value": "22a26060-c9d7-44b0-9f07-aa40488cfa3a"
        }
      ],
      "vid": 61574
    }
  ],
  "properties": {
    "website": {
      "value": "http: //hubspot.com",
      "versions": [
        {
          "timestamp": 1331075050646,
          "selected": false,
          "source-label": null,
          "value": "http: //hubspot.com",
          "source-type": "API",
          "source-id": null
        }
      ]
    },
    "city": {
      "value": "Cambridge",
      "versions": [
        {
          "timestamp": 1331075050646,
          "selected": false,
          "source-label": null,
          "value": "Cambridge",
          "source-type": "API",
          "source-id": null
        }
      ]
    },
    "firstname": {
      "value": "Adrian",
      "versions": [
        {
          "timestamp": 1331075050646,
          "selected": false,
          "source-label": null,
          "value": "Adrian",
          "source-type": "API",
          "source-id": null
        }
      ]
    },
    "zip": {
      "value": "02139",
      "versions": [
        {
          "timestamp": 1331075050646,
          "selected": false,
          "source-label": null,
          "value": "02139",
          "source-type": "API",
          "source-id": null
        }
      ]
    },
    "lastname": {
      "value": "Mott",
      "versions": [
        {
          "timestamp": 1331075050646,
          "selected": false,
          "source-label": null,
          "value": "Mott",
          "source-type": "API",
          "source-id": null
        }
      ]
    },
    "company": {
      "value": "HubSpot",
      "versions": [
        {
          "timestamp": 1331075050646,
          "selected": false,
          "source-label": null,
          "value": "HubSpot",
          "source-type": "API",
          "source-id": null
        }
      ]
    },
    "phone": {
      "value": "555-122-2323",
      "versions": [
        {
          "timestamp": 1331075050646,
          "selected": false,
          "source-label": null,
          "value": "555-122-2323",
          "source-type": "API",
          "source-id": null
        }
      ]
    },
    "state": {
      "value": "MA",
      "versions": [
        {
          "timestamp": 1331075050646,
          "selected": false,
          "source-label": null,
          "value": "MA",
          "source-type": "API",
          "source-id": null
        }
      ]
    },
    "address": {
      "value": "25FirstStreet",
      "versions": [
        {
          "timestamp": 1331075050646,
          "selected": false,
          "source-label": null,
          "value": "25FirstStreet",
          "source-type": "API",
          "source-id": null
        }
      ]
    },
    "email": {
      "value": "fumanchu@hubspot.com",
      "versions": [
        {
          "timestamp": 1331075050646,
          "selected": false,
          "source-label": null,
          "value": "fumanchu@hubspot.com",
          "source-type": "API",
          "source-id": null
        }
      ]
    }
  },
  "form-submissions": [
  ],
  "vid": 61574
}

假设JSON是如示例中格式的对象,那么一个简单的解决方案是:

awk '/^{/ {n++} n {print} n&&/^}/ {exit}'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM