简体   繁体   English

通过分隔符将字符串拆分为数组

[英]split String into Array by delimiter

I have a json data from API like below我有一个来自 API 的 json 数据,如下所示

{
    "message": "success",
    "data": [
        {
            "id": 1,
            "title": "SIPHON",
            "description": "<p><span style=\"color: #555555; font-family: Avenir-Regular; background-color: #ffffff;\">Siphon coffee was invented in the 1840s more or less simultaneously by a French housewife and Scottish marine engineer. It&rsquo;s been refined many times, but a few principles hold true: It produces a delicate, tea-like cup of coffee; it can be quite persnickety; and it is, for our money, one of the coolest brew methods available.</span></p>\r\n<p><span style=\"color: #555555; font-family: Avenir-Regular; background-color: #ffffff;\">Sumber : <a title=\"https://bluebottlecoffee.com/preparation-guides/siphon\" href=\"https://bluebottlecoffee.com/preparation-guides/siphon\" target=\"_blank\" rel=\"noopener\">https://bluebottlecoffee.com/preparation-guides/siphon</a></span></p>",
            "slug": "siphon",
            "image": "4cf25879ab69702dbe12c244f233f5b4_1551366184.jpg",
            "time": "{\"time1\":\"2\",\"time2\":\"3\",\"unit\":\"minute\"}",
            "temperature": "{\"temperature\":\"195\",\"unit\":\"\\u00b0F\"}",
            "ingredients": "[{\"name\":\"Coffee\",\"amount\":\"25\",\"unit\":\"gr\"},{\"name\":\"Hot Water\",\"amount\":\"300\",\"unit\":\"ml\"}]",
            "tools": "[{\"name\":\"Grinder\",\"amount\":\"1\",\"unit\":\"unit\"},{\"name\":\"Scale\",\"amount\":\"1\",\"unit\":\"unit\"},{\"name\":\"Siphon set\",\"amount\":\"1\",\"unit\":\"unit\"},{\"name\":\"Thermometer\",\"amount\":\"1\",\"unit\":\"unit\"},{\"name\":\"Timer\",\"amount\":\"1\",\"unit\":\"unit\"},{\"name\":\"Bamboo paddle\",\"amount\":\"1\",\"unit\":\"unit\"}]",
            "steps": "[\"After soaking your filter in a warm water bath for at least five minutes, drop it into the bottom of your siphon\\u2019s top component, or \\u201chopper,\\u201d and hook to the bottom of the hopper\\u2019s glass tubing.\",\"Fill your siphon\\u2019s bottom component, or \\u201cbulb,\\u201d with 300 grams of hot water (about a 12-oz. cup\\u2019s worth).\",\"Insert the hopper, filter and all, into the bulb. You don't have to press too hard; just make sure it's securely and evenly in place. Position the entire assembly above your heat source.\",\"While the water is heating, measure out between 20-25 grams of coffee and grind it just little bit finer than you would for regular drip coffee.\",\"Soon, the water in the bulb will begin boiling and rise up into the hopper. For some physics-related reason we don\\u2019t fully understand, a little bit will stay in the bottom. Don\\u2019t worry about this little bit.\",\"Once the water has moved into the hopper, turn your heat source down so that the water is between 185-195 degrees F.\",\"Add your coffee, and gently (but thoroughly) submerge it with a bamboo paddle or butter knife.\",\"Let the coffee brew, undisturbed, for one minute and 10 seconds.\",\"In one brisk motion, remove your siphon from its heat source and give it ten stirs with a bamboo paddle.\",\"Your coffee should take another minute or so to draw downward and finally rest in the bulb. You'll know it's ready when a dome of grounds has formed at the top of the filter, and when the coffee at the bottom has begun to bubble at approximately the pace and strength of a kitten\\u2019s heartbeat.\\r\\n\\r\\nRemove the hopper and serve. In order to guarantee the most complex cup, give the coffee a few minutes to cool.\"]",
            "step_images": "[\"4cf25879ab69702dbe12c244f233f5b4_11551366184.jpg\",\"4cf25879ab69702dbe12c244f233f5b4_21551366184.jpg\",\"4cf25879ab69702dbe12c244f233f5b4_31551366184.jpg\",\"4cf25879ab69702dbe12c244f233f5b4_41551366184.jpg\",\"4cf25879ab69702dbe12c244f233f5b4_51551366184.jpg\",\"4cf25879ab69702dbe12c244f233f5b4_61551366184.jpg\",\"4cf25879ab69702dbe12c244f233f5b4_71551366184.jpg\",\"4cf25879ab69702dbe12c244f233f5b4_81551366184.jpg\",\"4cf25879ab69702dbe12c244f233f5b4_91551366184.jpg\",\"4cf25879ab69702dbe12c244f233f5b4_101551366184.jpg\"]",
            "user_id": 1,
            "status_id": 1,
            "shared_id": 1,
            "created_at": "2019-02-28 15:03:04",
            "updated_at": "2019-02-28 15:03:04",
            "deleted_at": null
        }
    ]
}

What I want to do is split string in steps and store it in array.我想要做的是分拆分字符串并将其存储在数组中。 The delimiter is .\\", . Thanks.分隔符是.\\", . 谢谢。

You have to first parse the JSON by:您必须首先通过以下方式解析 JSON:

let obj = JSON.parse(yourJsonStringObject)

after that,在那之后,

let arrayOfSteps = obj.data[0].steps.split(".\")

steps is string so you can parse your output like this: steps是字符串,因此您可以像这样解析输出:

Let's say if you have stored the above json in variable apiResponse ;假设您已将上述 json 存储在变量apiResponse 中

Then get the parsed string:然后得到解析后的字符串:

var parsedResponse = JSON.parse(apiResponse["data"][0]["steps"]);

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

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