将Json响应保存到数组中的方法

编程入门 行业动态 更新时间:2024-10-26 16:27:03
本文介绍了将Json响应保存到数组中的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想将每个json响应保存在一个数组中,我确实这样写了

i want to save each json response in an array i did write like this

$(document).ready(function () { var saveJson = []; var jsonResponse = getjsonResponse() //something with $get function response json format saveJson = saveToArray(jsonResponse.SearchResults); var saveToArray = function(array){ $.each(array,function(i,data){ saveJson.push(data); }); }; });

但似乎是我的代码不起作用saveJson得到未定义我怎么能克服这个?我只想将json响应附加到一个数组对象。

but seems to be my code does not work saveJson getting undefined how can i overcome this? simply i want to append json response to one array object.

我的样本响应看起来像

"SearchResults":[ { "TypeFlag":"W", "HashCode":"66093013", "ShortenKey":"/k", "Title":"Yahoo! \u003cspan class=\"search-result-highlight\"\u003eSearch\u003c/span\u003e - Web \u003cspan class=\"search-result-highlight\"\u003eSearch\u003c/span\u003e", "Description":"The \u003cb\u003esearch\u003c/b\u003e engine that helps you find exactly what you\u0027re looking for. Find the most relevant information, video, images, and answers from all across the Web.", "Url":"search.yahoo/", "LastUpdateOn":"6/21/2011 1:01:11 PM", "PageRank":1, "ThumbImageUrl":"" }, { "TypeFlag":"W", "HashCode":"48394089", "ShortenKey":"/5i", "Title":"Lijit | Advertising Services, Audience Analytics and Publisher ...", "Description":"I’ve been using Lijit as my site \u003cb\u003esearch\u003c/b\u003e for several years and the understanding I get about my audience is critical to knowing what my readership is interested in and ...", "Url":"www.lijit/", "LastUpdateOn":"6/22/2011 11:31:41 PM", "PageRank":10, "ThumbImageUrl":"" } ]

谢谢

推荐答案

function(array){ $.each(array,function(i,data){ saveJson.push(data); }); };

返回 undefined 。虽然你将数据推送到saveJson,你的saveJson = saveToArray(jsonResponse)将saveJson重新指定为 undefined 。

returns undefined. Although you push data into saveJson, your "saveJson = saveToArray(jsonResponse)" re-assign the saveJson as undefined.

你可能有:

function(array){ var ret = []; $.each(array,function(i,data){ ret.push(data); }); return ret; };

更多推荐

将Json响应保存到数组中的方法

本文发布于:2023-10-29 13:08:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539779.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组中   方法   Json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!