jQuery将html解析为JSON [英] jQuery parsing html into JSON

查看:79
本文介绍了jQuery将html解析为JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用以下代码:

I am currently using the following code:

jQuery('#book-a-service').click(function(){
 var selectedServices = jQuery('.selected').parent().parent().html();       
 console.log(selectedServices);
});

并返回:

<td rowspan="3">Brakes</td>
  <td class="service-title">BRAKES SET</td>
  <td class="service-description"><p>Setting of front and rear brakes for proper functioning (excluding bleeding)</p></td>
  <td class="service-price">R <span id="price-brakes-set">R75</span><div id="select-brakes-set" class="select-service selected"></div>
</td>

这是我想要的,除了我需要一个包含'.selected'类的所有元素的数组在JSON ..我只想知道我几乎可以解析它的方式,我只得到td标签的内容和服务价格只有数值,然后我将如何插入这些值一个json对象?

which is what i want, except i need an array of all the elements with '.selected' class in JSON.. i just want to know how i could almost parse it in a way that i only get the contents of the td tags and as for the "service-price" only the numeric value and then how would i insert those values into a json object?

任何帮助大大赞赏..

Any Help Greatly Appreciated..

推荐答案

jQuery不是我最强大的框架,但这似乎可以解决问题。

jQuery is not my most formidable frameworks, but this seems to do the trick.

jQuery('#book-a-service').click(function(){
    var selected = jQuery('.selected');
    selected.each( function() {
        var children = jQuery(this).parent().parent().find('td');
        var json = {};
        console.log(children);
        json.type = jQuery(children[0]).text();
        json.title = jQuery(children[1]).text();
        json.description = jQuery(children[2]).find('p').text();
        json.price = jQuery(children[3]).find('span#price-brakes-set').text();
        console.log(json);
        console.log(JSON.stringify(json));
    });
});

行动中: http://jsfiddle.net/3n1gm4/DmYbb/

这篇关于jQuery将html解析为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆