简体   繁体   English

如何从JSON字符串解析数组?

[英]How to parse an array from a JSON string?

I have the following C# method: 我有以下C#方法:

[HttpPost]
public JsonResult GetDateBasedRegex(string date)
{
    string[] arr = CommonMethods.GetDateBasedRegex(date);

    JsonResult retVal = Json(JsonConvert.SerializeObject(retVal));

    return retVal;
}

arr contains the following: arr包含以下内容:

在此处输入图片说明

And retVal.Data contains this value: retVal.Data包含以下值:

在此处输入图片说明

That C# method is invoked by this JS: 该C#方法由此JS调用:

var date = $('#myDateField').val();

AjaxRequest(date, 'html', 'post', '/Common/GetDateBasedRegex', 'application/json;', function (response)
{
    var result = JSON.parse(response);
} 
);

I've set a breakpoint and added a watch for both response and result : 我设置了一个断点,并为responseresult添加了一个监视:

在此处输入图片说明

response is slightly longer because it escapes the quotes but otherwise they're the same value. response时间稍长一些,因为它转义了引号,否则它们的值相同。

Both variables are a single string. 这两个变量都是单个字符串。 How can I parse this value into an array? 如何将此值解析为数组? I looked at a variety of "suggested questions" by SO when typing this question, the most relevant one seems to be this: Javascript how to parse JSON array but I'm already doing what the selected answer suggests. 键入此问题时,我查看了SO提出的各种“建议问题”,其中最相关的似乎是: Javascript如何解析JSON数组,但我已经在按照所选答案提出建议了。

I tried making a JS fiddle: http://jsfiddle.net/zc2o6v52/ but due to the quotes added by the debugger I had to tweak the values slightly in order to get it to work. 我尝试制作一个JS小提琴: http//jsfiddle.net/zc2o6v52/,但是由于调试器添加了引号,因此我不得不稍稍调整这些值才能使其正常工作。 If I use the variable csharpJsonSerializedString or result the loop works but when I use response I get an "Uncaught SyntaxError: Unexpected string" error in my console. 如果我使用变量csharpJsonSerializedStringresult循环起作用,但是当我使用response时,我的控制台中出现“ Uncaught SyntaxError:Unexpected string”错误。

What am I doing wrong? 我究竟做错了什么? How do you parse an array from a JSON string? 您如何从JSON字符串解析数组?

EDIT 编辑

C# retval 's value: C# retval的值:

"[\\"^[0-9]{9}[A-Z0-9]{0,3}$\\",\\"^[AZ]{1,3}([0-9]{6}|[0-9]{9})$\\"]" “[\\” ^ [0-9] {9} [A-Z0-9] {0,3} $ \\ “\\” ^ [AZ] {1,3}([0-9] {6} | [0-9] {9})$ \\ “]”

JS response 's value: JS response的值:

""[\\"^[0-9]{9}[A-Z0-9]{0,3}$\\",\\"^[AZ]{1,3}([0-9]{6}|[0-9]{9})$\\"]"" “”[\\ “^ [0-9] {9} [A-Z0-9] {0,3} $ \\”,\\“^ [AZ] {1,3}([0-9] {6} | [0-9] {9})$ \\ “]””

JS result 's value: JS result的值:

"["^[0-9]{9}[A-Z0-9]{0,3}$","^[AZ]{1,3}([0-9]{6}|[0-9]{9})$"]" “[” ^ [0-9] {9} [A-Z0-9] {0,3} $ “” ^ [AZ] {1,3}([0-9] {6} | [O- 9] {9})$ “]”

Grabbed all of these by right click -> Copy value, so they include the 'wrapping' set of quotes to make it a string. 通过右键单击->复制值来获取所有这些内容,因此它们包括“包装”引号集使其成为字符串。

The C# looks like it's formed right for how one would define a JS array. C#看起来像是正确定义了JS数组的形式。 It seems like response is wrapped with an extra set of quotes though? 似乎response中包裹着一组额外的引号吗?

I also tried changing my JS to define var result = ['','']; 我还尝试过更改JS以定义var result = ['','']; outside of the ajax call, then to update its value with JSON.parse() but it still remains a single string afterwards. 在ajax调用之外,然后使用JSON.parse()更新其值,但此后仍保持为单个字符串。

Assuming this is the exact value your API returns on the POST request 假设这是您的API在POST请求中返回的确切值

"[\"^[0-9]{9}[A-Z0-9]{0,3}$\",\"^[A-Z]{1,3}([0-9]{6}|[0-9]{9‌​})$\"]"

This is no json, so JSON.parse can't work. 这不是json,因此JSON.parse无法正常工作。 Your JSON should look like this: 您的JSON应该如下所示:

[
"^[0-9]{9}[A-Z0-9]{0,3}$",
"^[A-Z]{1,3}([0-9]{6}|[0-9]{9‌​})$"
]

The best way would be to fix the backend to return the right value ( this might point you in the right direction). 最好的方法是修复后端以返回正确的值( 可能会为您指明正确的方向)。 Otherwise, you need to get rid of the quotation marks at the beginning and end which are added to your result variable: 否则,您需要删除添加到结果变量中的引号和结尾的引号:

var result = "\"[\"^[0-9]{9}[A-Z0-9]{0,3}$\",\"^[A-Z]{1,3}([0-9]{6}|[0-9]{9‌​})$\"]\"";
result = result.substring(1, result.length-1);
var jsonData = JSON.parse(result);
for (var i = 0; i < jsonData.length; i++) {
    console.log(jsonData[i]);
}

If that doesn't work, check how your response data in the ajax function differs from the valid json mentioned above. 如果这不起作用,请检查ajax函数中的响应数据与上述有效json有何不同。

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

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