用定界符将字符串拆分为数组

时间:2019-03-19 12:59:14

标签: javascript json

我有来自API的json数据,如下所示

class EmployerUserForm(ModelForm):
    company_name               = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Company Name'}))
    address                    = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Address'}))
    industry                   = forms.ChoiceField(choices = INDUSTRY,widget=forms.Select())
    isSalarySacrifies          = forms.ChoiceField(choices=SALARYSACRIFIES,widget=forms.RadioSelect())
    isNetpayRelief             = forms.ChoiceField(widget=forms.RadioSelect(),choices=NETPAY)
    contribution_type          = forms.ChoiceField(label="Contribution Type",choices = CONTRI_TYPE,widget=forms.Select())
    contribution_change_type   = forms.ChoiceField(label="Contribution Change Rate Type",choices = CONTRI_CHANGE_TYPE,widget=forms.Select())
    payrise_type               = forms.ChoiceField(label="Payrise",choices = PAYRISE,widget=forms.Select())
    bonus_type                 = forms.ChoiceField(label="Bonus",choices = BONUS,widget=forms.Select())
    class Meta:
        model = EmployerUser
        fields = [
            'company_name',
            'address',
            'industry',
            'isSalarySacrifies',
            'isNetpayRelief',
            'contribution_type',
            'contribution_change_type',
            'payrise_type',
            'bonus_type',
            ]

我想做的是在步骤中分割字符串并将其存储在数组中。分隔符为。\“,。谢谢。

2 个答案:

答案 0 :(得分:0)

您必须首先通过以下方式解析JSON:

let obj = JSON.parse(yourJsonStringObject)

之后

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

答案 1 :(得分:0)

步骤是字符串,因此您可以这样解析输出:

假设您是否将上述json存储在变量 apiResponse ;

然后获取解析的字符串:

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