简体   繁体   中英

Parse JSON string using node.js

I'm looking for some regex help on the following:

{ Start: '2019-10-21T15:00:00Z', End: '2019-10-21T15:30:00Z' }

I need to be able to just pull the start value from what is above. Ex: 2019-10-21T15:00:00Z

My regex is terrible and I don't even really have any semi-functional code to share.

Any help would be appreciated.

You didn't specify what tool you are using. Regex works slightly differently in Python, Perl, JavaScript, grep, etc.

For Perl, you need: Start: .\K[0-9TZ:-]+

To test this on the command-line:

echo "{ Start: '2019-10-21T15:00:00Z', End: '2019-10-21T15:30:00Z' }" |grep -Po 'Start: .\K[0-9TZ:-]+'

Couple of things

this is not valid JSON.

{ Start: '2019-10-21T15:00:00Z', End: '2019-10-21T15:30:00Z' }

This is valid JSON.

'{ "Start": "2019-10-21T15:00:00Z", "End": "2019-10-21T15:30:00Z" }'

You can parse this using JSON.parse() , which will parse your string into a javascript object.

var jsonString = '{ "Start": "2019-10-21T15:00:00Z", "End": "2019-10-21T15:30:00Z" }';

var parsedJson = JSON.parse(jsonString);
console.log(parsedJson.Start);
console.log(parsedJson.End);

you can check this, here is the example of JSON build & parse in java

https://github.com/tsc1989/JSON_BuildParse_Examples

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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