使用JSON.parse方法解析数据时,位置0错误的JSON中出现意外的令牌

时间:2018-01-30 09:52:39

标签: jquery json ajax asp.net-mvc parsing

这是从MVC控制器发送的字符串

'[{ "When": "", "Value": "NRMFS0131", "Text": "Achieve Montana" }]'

但是当我使用JSON.parse解析它时会抛出一个异常,说明位置0错误的JSON中的意外标记错误

请注意解析在chrome的控制台中完全正常

3 个答案:

答案 0 :(得分:1)

我在JSONLint / json验证器中运行你的json后,

这就是问题所在。

Error: Parse error on line 1:
'[{ "When": "", "Val
^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'

'不是JSON的有效第一个字符,它应该是'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['

您可以通过发送JSON来修复您的问题。由于您转换为任何其他类型,可能会出现'。希望我帮忙!

答案 1 :(得分:1)

开头和结尾的'个字符不是json的一部分。

此类情况的一般提示 - 始终在此处验证您的json https://jsonlint.com/。它会给你详细的错误信息。

答案 2 :(得分:0)

stringify json parse之前的var str = [{ "When": "??", "Value": "NRMFS0131", "Text": "Achieve Montana" }]; var Result=JSON.stringify(str); Result=JSON.parse(Result); console.log(Result);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Emgu.CV;
using Emgu.CV.Structure;

namespace Emgucv33Apps
{
public partial class FormCropImage : Form
{
    Image<Bgr, byte> imgInput;
    Rectangle rect;
    Point StartLocation;
    Point EndLcation;
    bool IsMouseDown = false;

    public FormCropImage()
    {
        InitializeComponent();
    }

    private void openToolStripMenuItem_Click(object sender, EventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog();
        if (ofd.ShowDialog()==DialogResult.OK)
        {
            imgInput = new Image<Bgr, byte>(ofd.FileName);
            pictureBox1.Image = imgInput.Bitmap;
        }
    }

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        IsMouseDown = true;
        StartLocation = e.Location;
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (IsMouseDown==true)
        {
            EndLcation = e.Location;
            pictureBox1.Invalidate();
        }
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        if (rect!=null)
        {
            e.Graphics.DrawRectangle(Pens.Red, GetRectangle());
        }
    }

    private Rectangle GetRectangle()
    {
        rect = new Rectangle();
        rect.X = Math.Min( StartLocation.X,EndLcation.X);
        rect.Y = Math.Min(StartLocation.Y, EndLcation.Y);
        rect.Width = Math.Abs(StartLocation.X - EndLcation.X);
        rect.Height = Math.Abs(StartLocation.Y - EndLcation.Y);

        return rect;
    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        if (IsMouseDown==true)
        {
            EndLcation = e.Location;
            IsMouseDown = false;
            if (rect!=null)
            {
                imgInput.ROI = rect;
                Image<Bgr, byte> temp = imgInput.CopyBlank();
                imgInput.CopyTo(temp);
                imgInput.ROI = Rectangle.Empty;
                pictureBox2.Image = temp.Bitmap;
            }
        }
    }
}
}