我有一个响应,其中包含存在于字符串(""
)中的数组。我需要的只是阵列。如何去除引号?
我尝试了JSON.parse()
。它给了我错误信息
SyntaxError:JSON中位置1处的意外令牌'
我所拥有的如下:
email_id: "['abc@mail.com', 'cde@mail.com']"
我需要的是:
email_id: ['abc@mail.com', 'cde@mail.com']
这是关键,这是我从我的angular 7应用程序中从后端获得的大量响应的一部分。
答案 0 :(得分:3)
如果字符串不包含public class Portal : MonoBehaviour
{
public Animator anim;
private bool inPortalRange;
// Public read-only access
public bool InPortalRange => inPortalRange;
// Reference each other via the Inspector in both portals
public Portal OtherPortal;
// Give this directly the according type so you don't need GetComponent at all
public PortalBorder portalBorder;
// I would recommend to do things always as early as possible
// Awake is executed before Start
private void Awake()
{
anim = GetComponent<Animator>();
// Instead of checking a bool in Update simply
// wait until the according events get invoked
portalBorder.OnEnteredPortalRange.AddListener(EnablePortal);
portalBorder.OnLeftPortalRange.AddListener(DisablePortal);
}
private void OnDestroy()
{
// always make sure to remove callbacks when not needed anymore
// in roder to avoid NullReferenceExceptions
portalBorder.OnEnteredPortalRange.RemoveListener(EnablePortal);
portalBorder.OnLeftPortalRange.RemoveListener(DisablePortal);
}
public void EnablePortal()
{
anim.SetBool("inPortalRange", true);
OtherPortal.anim.SetBool("inPortalRange", true);
}
public void DisablePortal()
{
anim.SetBool("inPortalRange", false);
OtherPortal.anim.SetBool("inPortalRange", false);
}
}
/'
考虑到其电子邮件数据,不可能使用该转义字符序列
答案 1 :(得分:1)
您必须在后端使用双引号(而不是一个引号)发送格式,或者仅在数组内部用双引号“ '
替换引号”,否则解析将失败,
请参见以下代码段:
let json = {
email_id: "['abc@mail.com', 'cde@mail.com']"
}
json.email_id = json.email_id.replace(/'/g,"\"");
console.log(JSON.parse(json.email_id));