查询/搜索 JSON 中的值
对于我正在创建的网站,我必须根据服务器以 JSON 字符串形式提供的数据创建报价。我一直在浏览这个网站(以及其他各个网站),但仍然不确定查询/搜索数据的最佳方式。
例如,我需要从区域 ID 中获取区域名称。我需要获取某个区域的最大年龄以及给定最小/最大年龄的价格。
我也想得到一系列的价格。
最好使用 eval 方法从字符串创建 Javascript 对象吗?或者我应该使用 jQuery。
感谢您的帮助。
({"SkiPass":[{"Id":1,"Area":"Chamonix","Rates":[{"Id":1,"AgeMin":0,"AgeMax":2,"Price":2.5},{"Id":2,"AgeMin":3,"AgeMax":17,"Price":5.0},{"Id":3,"AgeMin":18,"AgeMax":30,"Price":6.2},{"Id":4,"AgeMin":31,"AgeMax":59,"Price":7.4}]},
{"Id":2,"Area":"Megeve","Rates":[{"Id":1,"AgeMin":0,"AgeMax":2,"Price":1},{"Id":2,"AgeMin":3,"AgeMax":17,"Price":2.0},{"Id":3,"AgeMin":18,"AgeMax":30,"Price":2.2},{"Id":4,"AgeMin":31,"AgeMax":59,"Price":4.4}]},
{"Id":3,"Area":"Verbier","Rates":[{"Id":1,"AgeMin":0,"AgeMax":2,"Price":1.5},{"Id":2,"AgeMin":3,"AgeMax":17,"Price":3.0},{"Id":3,"AgeMin":18,"AgeMax":30,"Price":4.2},{"Id":4,"AgeMin":31,"AgeMax":59,"Price":5.4}]}]})
For a web site I'm creating, I have to create a quote based on data provided as a JSON string from the server. I've been looking through this site (and various others) but still am unsure on the best way to query/search the data.
For example, I need to get the Area Name from the Area ID. I need to get the maximum age for an area and also the price for a given minimum/maximum age.
I also want to get an array of prices.
Is it best to create a Javascript object from the string using the eval method? Or should I be using jQuery.
Thanks for your help.
({"SkiPass":[{"Id":1,"Area":"Chamonix","Rates":[{"Id":1,"AgeMin":0,"AgeMax":2,"Price":2.5},{"Id":2,"AgeMin":3,"AgeMax":17,"Price":5.0},{"Id":3,"AgeMin":18,"AgeMax":30,"Price":6.2},{"Id":4,"AgeMin":31,"AgeMax":59,"Price":7.4}]},
{"Id":2,"Area":"Megeve","Rates":[{"Id":1,"AgeMin":0,"AgeMax":2,"Price":1},{"Id":2,"AgeMin":3,"AgeMax":17,"Price":2.0},{"Id":3,"AgeMin":18,"AgeMax":30,"Price":2.2},{"Id":4,"AgeMin":31,"AgeMax":59,"Price":4.4}]},
{"Id":3,"Area":"Verbier","Rates":[{"Id":1,"AgeMin":0,"AgeMax":2,"Price":1.5},{"Id":2,"AgeMin":3,"AgeMax":17,"Price":3.0},{"Id":3,"AgeMin":18,"AgeMax":30,"Price":4.2},{"Id":4,"AgeMin":31,"AgeMax":59,"Price":5.4}]}]})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
毫无疑问,从字符串创建一个 JavaScript 对象,但要使用合法的 JSON 解析工具而不是“eval()”。您可以使用 jQuery,但还有其他解决方案,例如 json.org,小而简单。
一旦它是一个 JavaScript 对象,那么您的需求应该指导您是否需要某些查询解决方案,或者这只是一个简单的编程问题。
Create a JavaScript object from the string, most definitely, but do it with legitimate JSON parsing facilities and not "eval()". You could use jQuery, but there are other solutions, such as the JSON tools available from json.org, which are small and simple.
Once it's a JavaScript object, well then your needs should guide you as to whether some query solution is necessary, or instead that it's just a simple matter of programming.
我认为最好的方法是 jLinq: http://hugoware.net/Projects/jLinq 这就像做一个JSON 上的 SQL 查询。
它不需要 jQuery。
我用它,它很棒。
I think the best method is jLinq: http://hugoware.net/Projects/jLinq it's like doing a SQL query on JSON.
It doesn't needs jQuery.
I use it, and it's great.
如果您已经在使用 jQuery,请使用 JSON.parse() 或 jQuery.parseJSON() 从 JSON 字符串创建对象,或者只是从服务器端将其作为 JSON 传递。
然后,您可以迭代该对象以查找所需的记录。或者,您可以使用构建对象,以便自然地从它们中获取数据。
Create the object from the JSON string using JSON.parse() or jQuery.parseJSON() if you are already using jQuery -- or just pass it as from the server side as JSON.
You can then iterate through the object to find the record you want. Or, you can use build your objects so that you can naturally grab data from them.
FloatLeft - 正如 Dan 指出的那样,如果您可以使用 XPath,您的任务会容易得多,但无需以 XML 格式重新编写数据。借助 DefiantJS (http://defiantjs.com),您现在可以使用 XPath 表达式查询 JSON 结构。
DefiantJS 使用“search”方法扩展了全局对象 JSON,该方法启用 XPath 查询并返回包含匹配项的数组(如果未找到匹配项,则返回空数组)。返回的数组也配备了聚合函数;其中“sortDesc”之一。
看看这个工作小提琴;
http://jsfiddle.net/hbi99/H3PR3/
FloatLeft - as Dan points out, your task would be much easier if you could use XPath but there is no need to re-write your data in XML format. With DefiantJS (http://defiantjs.com) you can now query JSON structure with XPath expressions.
DefiantJS extends the global object JSON with the method "search", which enables XPath queries and returns an array with the matches (empty array if no matches were found). The returned array is equipped with aggregate functions as well; one of these "sortDesc".
Check out this working fiddle;
http://jsfiddle.net/hbi99/H3PR3/