简体   繁体   English

将 JSON 字符串转换为 JSON Object 中的 Z686155AF75A60A0F36E9D80E19F

[英]Convert JSON String to JSON Object in JavaScript

I understand there are many similar questions exist, however I couldn't find a proper answer.我知道存在许多类似的问题,但是我找不到正确的答案。

Consider the Javascript object,考虑 Javascript object,

var a = [ { defaultCount:'200' , timeintervals : [{ day:'Weekday', from:'9:00', to:'18:00', count:'100' }, { day:'Weekday', from:'9:00', to:'18:00', count:'100' } ]}];

Now running a[0].defaultCount gives me "200" as expected.现在运行a[0].defaultCount按预期给我“200”。

Now the same Javascript object, I am putting inside double quotes.现在同样的 Javascript object,我放在双引号内。

var b = "[ { defaultCount:'200' , timeintervals : [{ day:'Weekday', from:'9:00', to:'18:00', count:'100' }, { day:'Weekday', from:'9:00', to:'18:00', count:'100' } ]}]";

here b[0] gives me "[" and b[4] gives me "d" so on.这里 b[0] 给了我“[”,而 b[4] 给了我“d”,依此类推。

If I understand correctly, var a is JavaScript Object and var b is String.如果我理解正确,var a 是 JavaScript Object 而 var b 是字符串。 I am receiving var b from the Java side as a String.我从 Java 端接收 var b 作为字符串。 How do I convert var b into a form that I can iterate as I do on var a?如何将 var b 转换为可以像在 var a 上一样迭代的形式?

I have tried: var c = JSON.parse(b) but getting syntax error.我试过: var c = JSON.parse(b)但出现语法错误。

I really do not wish to change the JSON format much as it is the template used in the Java end as well.我真的不想更改 JSON 格式,因为它也是 Java 端使用的模板。 Are there any easy workarounds?有没有简单的解决方法?

console screenshot控制台截图

[{ 
  "defaultCount":"200" , 
  "timeintervals": [{ "day":"Weekday", "from":"9:00", "to":"18:00", "count":"100" }, { "day":"Weekday", "from":"9:00","to":"18:00", "count":"100"}]
}]

use double quotes使用双引号

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

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