简体   繁体   English

jQuery将html解析为JSON

[英]jQuery parsing html into JSON

I am currently using the following code: 我目前正在使用以下代码:

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

and that returns: 并返回:

<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>

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? 这就是我想要的,除了我需要一个包含JSON中'.selected'类的所有元素的数组。我只想知道我几乎可以用一种方式解析它,我只得到td标签的内容和至于“服务价格”只有数值,然后我将如何将这些值插入到json对象?

Any Help Greatly Appreciated.. 任何帮助大大赞赏..

jQuery is not my most formidable frameworks, but this seems to do the trick. jQuery不是我最强大的框架,但这似乎可以解决问题。

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));
    });
});

in action: http://jsfiddle.net/3n1gm4/DmYbb/ 在行动: http//jsfiddle.net/3n1gm4/DmYbb/

When various elements share the same class and you select them with $(".class"), you can iterate through all of them using: 当各种元素共享同一个类并使用$(“。class”)选择它们时,您可以使用以下方法遍历所有元素:

$(".selected").each(function() {
    var element = $(this); // This is the object with class "selected" being used in this iteration
    var absoluteParent = $(this).parent().parent();

    // Do whatever you want...

    var title_element = $(".service-title", absoluteParent); // Get service-title class elements in the context provided by "absoluteParent", I mean, "inside" of absoluteParent
    var title = title_element.html();
});

In the specific case of prices, I don't know exactly what is the price (probably R75?). 在价格的具体情况下,我不知道究竟是什么价格(可能是R75?)。 Anyway, it should be inside a div and then select that div to obtain the price. 无论如何,它应该在div内,然后选择该div来获得价格。 If it is R75, then note that the "id" property should be unique for every DOM object in your HTML. 如果它是R75,那么请注意,对于HTML中的每个DOM对象,“id”属性应该是唯一的。

Also note that, when getting HTML, you're only getting a string , not the actual element, so it will probably not be useful for getting new values in an easy way (you won't be able to navigate through DOM elements with an ordinary string, even if it represents HTML from an actual object). 还要注意,在获取HTML时,你只获得一个字符串 ,而不是实际的元素,所以它可能对于以简单的方式获取新值没有用(你将无法使用普通字符串,即使它表示来自实际对象的HTML)。 Always get jQuery objects and work with them, unless you actually need the HTML. 总是得到jQuery对象并使用它们,除非你真的需要HTML。

For generating a JSON string, just create a global array and add the objects/values you need there. 要生成JSON字符串,只需创建一个全局数组并添加所需的对象/值。 Then you can obtain a JSON string using var jsonText = JSON.stringify(your_array); 然后,您可以使用var jsonText = JSON.stringify(your_array);获取JSON字符串var jsonText = JSON.stringify(your_array);

Consider not doing this in Javascript, as it's not useful in the majority of cases. 考虑不要在Javascript中这样做,因为它在大多数情况下都没用。 Just send the values through POST value to a script (PHP, for example) and in the PHP you will get the actual value. 只需将值通过POST值发送到脚本(例如PHP),然后在PHP中获取实际值。 The other way (PHP to Javascript) will be useful to return JSON (using json_encode($a_php_array) ) and then, in Javascript, transform to a JS array using var my_array = JSON.parse(the_string_returned_by_php); 另一种方式(PHP到Javascript)将有用于返回JSON(使用json_encode($a_php_array) )然后,在Javascript中,使用var my_array = JSON.parse(the_string_returned_by_php);转换为JS数组var my_array = JSON.parse(the_string_returned_by_php); .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM