2016-02-26 61 views
0

我需要从json字符串创建JSON对象。我试图JSON.parse(json-string)但我得到的错误:流星从json字符串创建JSON对象

SyntaxError: Unexpected token o

但是当我运行在Android的JSONObject(json-string)相同的字符串,我得到一个没有错误

HTTP.get(url, function (error, response) { 

if (response) { 

    var content = JSON.parse(response); 
    console.log(content); 

难道我做错了什么? response是否包含json-string而没有其他未修改?谢谢

+0

什么是您的JSON是什么样子? –

+0

它包含单引号,当我在终端中使用console.log时,它在每个单引号前加上“\”。 –

回答

0

response是一个对象。您需要JSON.parse(response.content)或仅需response.data

official docs

Contents of the result object:

statusCodeNumber

Numeric HTTP result status code, or null on error.

contentString

The body of the HTTP response as a string.

dataObject or null

If the response headers indicate JSON content, this contains the body of the document parsed as a JSON object.

headersObject

A dictionary of HTTP headers from the response.

+1

实际上,响应标头指示JSON内容。这是否意味着我不需要执行'JSON.parse(response.content)',只是使用“数据”,如果是这样,它是如何完成的? :) thx –

+0

hm ...是的。好像你可以用'response.data'代替) – imkost

+0

有没有流星核心JSON API,我找不到,但EJSON。我需要提取一些数组和一些对象我的名字。 –