0

我想从 JSON 文件中删除风味和 pkgT 字段,每行的格式如下:

     "jobID":2598752", "account":"TG-CCR120014", "user":"charngda", "pkgT":{"mvapich2-new/1.2":{"libA":["libmpich.so.1.1"], "flavor":["default:pgi/7.2-5"]}}, "startEpoch":"1338608868", "runTime":"48", "execType":"user:binary", "exec":"IOR", "numNodes":"4", "sha1":"755187bd8550881bb0c9951822e74a9a53c8d0f3", "execEpoch":1336757832, "execModify":"Fr, Ma, 1, 12:37:1, 2012", "startTime":"Fr, Ju,  , 22:47:4, 2012", "numCores":"64", "sizeT":{"bss":"36224", "text":"3502656", "data":"128944"}}, 

那么如何构造一个正则表达式来删除这些字段呢?谢谢!!!!

好的,我修复了 JobID 字段中缺少的报价,现在怎么办?

4

1 回答 1

0

既然你的字符串已经在 J​​SON 中了,为什么还要使用正则表达式呢?这只是使问题过于复杂。相反,获取您的字符串并使用带有JSON.parse().

var str = '{ "jobID": "2598752", "account": "TG-CCR120014", "user": "charngda", "pkgT": { "mvapich2-new/1.2": { "libA": ["libmpich.so.1.1"], "flavor": ["default:pgi/7.2-5"] } }, "startEpoch": "1338608868", "runTime": "48", "execType": "user:binary", "exec": "IOR", "numNodes": "4", "sha1": "755187bd8550881bb0c9951822e74a9a53c8d0f3", "execEpoch": 1336757832, "execModify": "Fr, Ma, 1, 12:37:1, 2012", "startTime": "Fr, Ju,  , 22:47:4, 2012", "numCores": "64", "sizeT": { "bss": "36224", "text": "3502656", "data": "128944" } }';
var obj = JSON.parse(str);

然后继续删除密钥。

delete obj['flavor'];
delete obj['pkgT'];
//returns true if deleted, false if failed

然后您可以将 JSON 转换回字符串。

var newstr = JSON.stringify(obj);
于 2013-04-11T05:30:42.743 回答