简体   繁体   English

使用jQuery将HTML文本刮到JSON中,但由于循环引用而无法进行字符串化

[英]Scraping HTML text into JSON with jQuery, but cannot stringify because of circular reference

This seems like a fairly common question, but I can't quite figure out what's going on in my situation. 这似乎是一个相当普遍的问题,但我无法弄清楚在我的情况下发生了什么。

The question " Stringify (convert to JSON) a JavaScript object with circular reference " explains that JSON.stringify can accept a filter function to help get around the circular references problem. 问题“ 使用循环引用对一个JavaScript对象进行字符串化(转换为JSON) ”解释了JSON.stringify可以接受一个过滤函数来帮助解决循环引用问题。 I don't understand why my references are circular as I am creating new, simple objects. 我不明白为什么我的引用是循环的,因为我正在创建新的简单对象。

Let's say you have an HTML structure like this: 假设你有一个像这样的HTML结构:

<div class="oauth-permitted-apps">
    <div class="oauth-client">
        <span class="client_type">Web App</span>
        <span class="client_name">Name</span>
        <span class="client_secret">client_guid1</span>
    </div>
    <div class="oauth-client">
        <span class="client_type">Installed App</span>
        <span class="client_name">Name2</span>
        <span class="client_secret">client_guid2</span>
    </div>
    <div class="oauth-client">
        <span class="client_type">Installed App</span>
        <span class="client_name">Name3</span>
        <span class="client_secret">client_guid3</span>
    </div>
    <!-- and so on -->
</div>

With the following JavaScript: 使用以下JavaScript:

var result = $('.oauth2-client')
    .filter( function() {
        return $(this).find('.client_type').text().toLowerCase().indexOf('installed') != -1;
    })
    .map(function() {
        return json = {
            id: $(this).find('.client_id').text(),
            name: $(this).find('.client_name').text()
        };
    });
JSON.stringify(result);

I would expect to see something like: 我希望看到类似的东西:

[
    {
        id: "client_guid2",
        name: "Name2"
    },
    {
        id: "client_guid3",
        name: "Name3"
    }
]

But... I have a circular reference. 但是......我有一个循环参考。 I don't understand why a new JavaScript object with only two properties does this. 我不明白为什么只有两个属性的新JavaScript对象会这样做。 I'm guessing there's something going on with jQuery that I don't quite understand? 我猜测jQuery有什么东西我不太明白?

(Context: working on connecting to a web service using OAuth in a Windows Store App. They've made it a pain (the user has to provide JSON files to us, open web browsers a few times, and copy-paste a token within 60 seconds of generating it) so we'd like to automate the process for the user a bit. I've done things with a WebView that I'm not too proud of. It's a shame I cannot use window.external.notify because Windows 8.1 requires you to add the domains to the app manifest, but we don't know the domains to add!) (上下文:在Windows应用商店应用中使用OAuth连接到Web服务。他们很痛苦(用户必须向我们提供JSON文件,打开Web浏览器几次,并在内部复制粘贴令牌) 60秒生成它)所以我们想为用户自动化一下这个过程。我已经用WebView做了一些我不太自豪的事情。很遗憾我不能使用window.external.notify因为Windows 8.1要求您将域添加到应用程序清单,但我们不知道要添加的域!)

What you have is actually a jQuery object containing an array. 你拥有的实际上是一个包含数组的jQuery对象。 You'll want to unwrap the array from the jquery object using .get() . 您将要使用.get()从jquery对象中解包该数组。

JSON.stringify(result.get());

As mentioned in comments, Using $.map is an option too, though i find it to be uglier in this case than the other way of doing it. 正如评论中所提到的,使用$.map也是一个选项,虽然我发现在这种情况下它比其他方式更加丑陋。

var result = $.map(
    $('.oauth2-client').filter(function () {
        return $(this).find('.client_type').text().toLowerCase().indexOf('installed') != -1;
    }), 
    function (el) {
        return json = {
            id: $(el).find('.client_id').text(),
            name: $(el).find('.client_name').text()
        };
    });
JSON.stringify(result);

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

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