【问题标题】:How to get the first json code between a text including 3 json codes如何在包含 3 个 json 代码的文本之间获取第一个 json 代码
【发布时间】:2021-05-20 13:03:00
【问题描述】:

谁能帮助我使用正则表达式来获取包含 1 个或多个代码的文本中的第一个 json 代码?

我正在尝试使用以下代码:{([^']+)} 但这会得到代码中的所有 jsons

我要获取json中第一个代码的所有文件都有一个或多个代码块,我只需要获取第一个,也就是“Item Definition”后面的代码

带有另外 2 个 json 的文本:

ItemDefinition
{ // I'm trying to get the code from here  
    "itemid": 590532217,
    "shortname": "ammo.nailgun.nails",
    "displayName": {
        "token": "ammo.nailgun.nails",
        "english": "Nailgun Nails"
    },
    "displayDescription": {
        "token": "ammo.nailgun.nails.desc",
        "english": "Standard nailgun ammunition"
    },
    "iconSprite": {
        "instanceID": 148798
    },
    "category": 8,
    "selectionPanel": 0,
    "maxDraggable": 0,
    "itemType": 1,
    "amountType": 0,
    "occupySlots": 0,
    "stackable": 64,
    "quickDespawn": false,
    "rarity": 0,
    "spawnAsBlueprint": false,
    "inventorySelectSound": {
        "instanceID": 115050
    },
    "inventoryGrabSound": {
        "instanceID": 115050
    },
    "inventoryDropSound": {
        "instanceID": 81228
    },
    "physImpactSoundDef": {
        "instanceID": 60196
    },
    "condition": {
        "enabled": false,
        "max": 0.0,
        "repairable": false,
        "maintainMaxCondition": false,
        "foundCondition": {
            "fractionMin": 1.0,
            "fractionMax": 1.0
        }
    },
    "hidden": false,
    "flags": 0,
    "steamItem": {
        "instanceID": 0
    },
    "Parent": {
        "instanceID": 0
    },
    "worldModelPrefab": {
        "guid": "d8c823436ceda0f42a4da54a972807bf"
    },
    "Traits": 8,
    "panel": {
        "instanceID": 0
    }
} // Until here

ItemModProjectile
{
    ...other json code
}

【问题讨论】:

  • s.match(/^{$([^]*?)^}$/m)[1]
  • 谢谢@WiktorStribiżew sz

标签: javascript json regex search text


【解决方案1】:

虽然您可以使用正则表达式来解析 JSON,但它并不可靠,并且会在极端情况下失败。

您可以使用此函数提取第一个 JSON 块,并对其进行解析:

function parseFirstJson(text) {
    try {
        const regex = /^[^\{]*([\s\S]*?[\r\n]\})[\s\S]*$/;
        let obj = JSON.parse(text.replace(regex, '$1'));
        return obj;
    } catch(e) {
        // return JSON parse error
        let obj = { error: e.toString() }; 
        return obj;
    }
}

const text =`ItemDefinition
{
    "itemid": 590532217,
    "shortname": "ammo.nailgun.nails",
    "displayName": {
        "token": "ammo.nailgun.nails",
        "english": "Nailgun Nails"
    }
}

ItemModProjectile
{
    "more": "stuff"
}`;

let obj = parseFirstJson(text);
console.log(JSON.stringify(obj, null, ' '));

输出:

{
 "itemid": 590532217,
 "shortname": "ammo.nailgun.nails",
 "displayName": {
  "token": "ammo.nailgun.nails",
  "english": "Nailgun Nails"
 }
}

解释:

  • ^ - 字符串开头
  • [^\{]* - 扫描到第一个 { 之前
  • ( - 捕获组的开始
    • [\s\S]*? - 仔细扫描所有字符,直到:
    • [\r\n]\} - 行首的}
  • ) - 捕获组结束
  • [\s\S]*$ - 扫描其他所有内容,直到行尾

【讨论】:

  • @Sasuke:有什么问题吗?这符合您的需求吗?
  • @Sasuke:愿意接受答案吗?帮助是双向的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-13
  • 2013-08-11
  • 2014-12-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多