3

我需要从外部域获取 json 数据。我使用 webrequest 从网站获取响应。继承人的代码:

var search = umbraco.library.Request("search");
string Url = "http://ff.ss.dk/Suggest.ff?username=us&password=pass&channel=dk&format=JSON&query="+search;
WebRequest webRequest = WebRequest.Create(Url);
WebResponse response = webRequest.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);

这样我得到这样的输出

[{"hitCount":0,"imageURL":"","query":"Atrix h\u00E5ndcreme Dobbeltvirkende /100 Ml","type":"productName"},{"hitCount":0,"imageURL": "","query":"V\u00E6gur 磁吸","type":"productName"},{"hitCount":0,"imageURL":"","query":"Bic kuglepen Atlantis , bl\u00E5 ","type":"productName"},{"hitCount":0,"imageURL":"","query":"Laminering AT1256 31cm x30m A3","type":"productName"}]

我想以 JSON 对象格式获取此输出数据,我可以在我的 java 脚本函数中使用它。我认为输出数据的格式不合适,因为我使用了流式读取器功能来获取数据。有什么想法可以解决这个问题吗?

4

2 回答 2

7

可以使用 jQuery.parseJSON 将此数据转换为 json 格式。

 var obj = JSON.parse(data);

然后我们可以访问数据

 obj[0].id

谢谢大家。

于 2012-08-10T05:56:53.340 回答
0

您似乎缺少数组名称说明符,例如,这有效:

{arrayName:[{"hitCount":0,"imageURL":"","query":"Atrix h\u00E5ndcreme Dobbeltvirkende /100 Ml","type":"productName"},{"hitCount":0,"imageURL":"","query":"V\u00E6gur magnetisk attraction","type":"productName"},{"hitCount":0,"imageURL":"","query":"Bic kuglepen Atlantis , bl\u00E5","type":"productName"},{"hitCount":0,"imageURL":"","query":"Laminering AT1256 31cm x30m A3","type":"productName"}]}
于 2012-08-07T12:36:46.880 回答