将每个字符解析为对象的JSON字符串

将每个字符解析为对象的JSON字符串,json,Json,我有一个JSON文件,其中包含我认为正确的JSON字符串: {"title": "exampleTitle", "tipTitle": "exampleTipTitle", "tip": "exampleTip"} 我试图解析所述文件并取出3个值,然后将它们存储在变量中,但目前,它将每个字符解析为单独的对象,因此: JSONobj[1] = " 等等。假设currentLocation=json文件的目录位置 代码 var jsonLocation = currentLocation +

我有一个JSON文件,其中包含我认为正确的JSON字符串:

{"title": "exampleTitle", "tipTitle": "exampleTipTitle", "tip": "exampleTip"}
我试图解析所述文件并取出3个值,然后将它们存储在变量中,但目前,它将每个字符解析为单独的对象,因此:

JSONobj[1] =  "
等等。假设currentLocation=json文件的目录位置

代码

var jsonLocation = currentLocation + "json.txt";
var request = new XMLHttpRequest();
request.open("GET", jsonLocation, false);
request.send(null);
var returnValue = request.responseText;
var JSONobj = JSON.parse(JSON.stringify(returnValue));
var headerTitle = JSONobj[0];
经过几次澄清,stringify出现了,因为它抛出了一个意外的令牌错误。我尝试将文件tile改为.json,但这也没有什么区别。“当主线程被弃用时,它也会发出一个XMLHttpRequest”,但我不太确定如何解决这个问题。任何帮助都将不胜感激

这里的
returnValue
是一个JSON字符串

"{\"title\": \"exampleTitle\", \"tipTitle\": \"exampleTipTitle\", \"tip\": \"exampleTip\"}
"{\"title\": \"exampleTitle\", \"tipTitle\": \"exampleTipTitle\", \"tip\": \"exampleTip\"}
这里将JSON字符串转换为JSON。因此,有一个JSON字符串表示一个字符串,该字符串表示JSON中的数据结构

"\"{\\"title\\": \\"exampleTitle\\", \\"tipTitle\\": \\"exampleTipTitle\\", \\"tip\\": \\"exampleTip\\"}"
然后解析它并将其转换回JSON的原始字符串

"{\"title\": \"exampleTitle\", \"tipTitle\": \"exampleTipTitle\", \"tip\": \"exampleTip\"}
"{\"title\": \"exampleTitle\", \"tipTitle\": \"exampleTipTitle\", \"tip\": \"exampleTip\"}
所以你回到了起点

只要不要在这里使用
JSON.stringify
,您就可以将JSON转换为JavaScript对象:

var javascript_object = JSON.parse(returnValue);

然后您有一个对象,但它没有
0
属性,因此使用
javascript\u对象[0]
访问它没有意义。属性具有名称,例如
javascript\u object.title

这里的
returnValue
是一个JSON字符串

"{\"title\": \"exampleTitle\", \"tipTitle\": \"exampleTipTitle\", \"tip\": \"exampleTip\"}
"{\"title\": \"exampleTitle\", \"tipTitle\": \"exampleTipTitle\", \"tip\": \"exampleTip\"}
这里将JSON字符串转换为JSON。因此,有一个JSON字符串表示一个字符串,该字符串表示JSON中的数据结构

"\"{\\"title\\": \\"exampleTitle\\", \\"tipTitle\\": \\"exampleTipTitle\\", \\"tip\\": \\"exampleTip\\"}"
然后解析它并将其转换回JSON的原始字符串

"{\"title\": \"exampleTitle\", \"tipTitle\": \"exampleTipTitle\", \"tip\": \"exampleTip\"}
"{\"title\": \"exampleTitle\", \"tipTitle\": \"exampleTipTitle\", \"tip\": \"exampleTip\"}
所以你回到了起点

只要不要在这里使用
JSON.stringify
,您就可以将JSON转换为JavaScript对象:

var javascript_object = JSON.parse(returnValue);

然后您有一个对象,但它没有
0
属性,因此使用
javascript\u对象[0]
访问它没有意义。属性具有名称,例如
javascript\u object.title

这里的
returnValue
是一个JSON字符串

"{\"title\": \"exampleTitle\", \"tipTitle\": \"exampleTipTitle\", \"tip\": \"exampleTip\"}
"{\"title\": \"exampleTitle\", \"tipTitle\": \"exampleTipTitle\", \"tip\": \"exampleTip\"}
这里将JSON字符串转换为JSON。因此,有一个JSON字符串表示一个字符串,该字符串表示JSON中的数据结构

"\"{\\"title\\": \\"exampleTitle\\", \\"tipTitle\\": \\"exampleTipTitle\\", \\"tip\\": \\"exampleTip\\"}"
然后解析它并将其转换回JSON的原始字符串

"{\"title\": \"exampleTitle\", \"tipTitle\": \"exampleTipTitle\", \"tip\": \"exampleTip\"}
"{\"title\": \"exampleTitle\", \"tipTitle\": \"exampleTipTitle\", \"tip\": \"exampleTip\"}
所以你回到了起点

只要不要在这里使用
JSON.stringify
,您就可以将JSON转换为JavaScript对象:

var javascript_object = JSON.parse(returnValue);

然后您有一个对象,但它没有
0
属性,因此使用
javascript\u对象[0]
访问它没有意义。属性具有名称,例如
javascript\u object.title

这里的
returnValue
是一个JSON字符串

"{\"title\": \"exampleTitle\", \"tipTitle\": \"exampleTipTitle\", \"tip\": \"exampleTip\"}
"{\"title\": \"exampleTitle\", \"tipTitle\": \"exampleTipTitle\", \"tip\": \"exampleTip\"}
这里将JSON字符串转换为JSON。因此,有一个JSON字符串表示一个字符串,该字符串表示JSON中的数据结构

"\"{\\"title\\": \\"exampleTitle\\", \\"tipTitle\\": \\"exampleTipTitle\\", \\"tip\\": \\"exampleTip\\"}"
然后解析它并将其转换回JSON的原始字符串

"{\"title\": \"exampleTitle\", \"tipTitle\": \"exampleTipTitle\", \"tip\": \"exampleTip\"}
"{\"title\": \"exampleTitle\", \"tipTitle\": \"exampleTipTitle\", \"tip\": \"exampleTip\"}
所以你回到了起点

只要不要在这里使用
JSON.stringify
,您就可以将JSON转换为JavaScript对象:

var javascript_object = JSON.parse(returnValue);


然后您有一个对象,但它没有
0
属性,因此使用
javascript\u对象[0]
访问它没有意义。属性有名称,例如
javascript\u object.title

您的JSON不描述数组,因此使用
0
之类的索引对其进行索引是没有意义的。您的JSON描述了一个对象,该对象的属性名为
title
tipTitle
、和
tip

此外,您的解析做得太多了:您只想解析,而不是字符串化(这与解析相反):

因此:



旁注:当您将它分配给您调用的变量
JSONobj
时,它不再是JSON,它只是一个普通的JavaScript对象,所以这个名称有点误导。如果您正在编写源代码,并且您没有处理字符串,那么您就不再处理JSON了。:-)

您的JSON不描述数组,因此使用类似
0
的索引对其进行索引是没有意义的。您的JSON描述了一个对象,该对象的属性名为
title
tipTitle
、和
tip

此外,您的解析做得太多了:您只想解析,而不是字符串化(这与解析相反):

因此:



旁注:当您将它分配给您调用的变量
JSONobj
时,它不再是JSON,它只是一个普通的JavaScript对象,所以这个名称有点误导。如果您正在编写源代码,并且您没有处理字符串,那么您就不再处理JSON了。:-)

您的JSON不描述数组,因此使用类似
0
的索引对其进行索引是没有意义的。您的JSON描述了一个对象,该对象的属性名为
title
tipTitle
、和
tip

此外,您的解析做得太多了:您只想解析,而不是字符串化(这与解析相反):

因此:



旁注:当您将它分配给您调用的变量
JSONobj
时,它不再是JSON,它只是一个普通的JavaScript对象,所以这个名称有点误导。如果您正在编写源代码,并且您没有处理字符串,那么您就不再处理JSON了。:-)

您的JSON不描述数组,因此使用类似
0
的索引对其进行索引是没有意义的。您的JSON描述了一个对象,该对象的属性名为
title
tipTitle
、和
tip

此外,您的解析做得太多了:您只想解析,而不是stringify(这是