1

API呼び出しを投稿すると、このhtmlが応答として取得されるので、jqueryまたはいくつかの単純なjavascriptコードでclient_idsession_tokenを読み取る方法を知りたいですか??

function getNewsFeed(d) {
    var c = {
        page_num: b,
        num_events: d,
        return_as_html: true,
        source: "web"
    };
    TAGGED.api.call(c, check)
}

今物事をチェックしたい:

function check() {
    // checking here
}

["{\"stats\":\"ok\",\"dia\":\"44\",\"result\":{\"success\":true,\"session_token\":\" 969ndg1ai0c43034\",\"next_event_id\":0,\"client_id\":1314852}}"]

私はJSONを持っていないので、JSON.parseでそれを読むこと ができません

firebugでチェックすると、応答とhtmlに同じコードが表示されます。

ありがとう。

4

1 に答える 1

2

値に特定の特殊文字が含まれないことが確実な場合は、カンマで分割し、次にコロンで分割して、一致しない文字を取り除くことができます...

str = '["{\"stats\":\"ok\",\"dia\":\"44\",\"result\":{\"success\":true,\"session_token\":\"969ndg1ai0c43034\",\"next_event_id\":0,\"client_id\":1314852}}"]'

// replace all characters that are not letters, numbers, underscore, colon or comma
str = str.replace(/[^a-zA-Z0-9_:,]/g,'')

// get array of key value pairs
all = str.split(',')

// create empty opts object to store final data
opt = {}

// loop through array of key value pairs
for(i in all){

    // split into key value array
    bits = all[i].split(/:/)

    // assign to opt object
    opt[bits[0]] = bits[1]

}

// access values vie keys of opt
alert(opt.client_id+": "+opt.session_token)
于 2012-10-14T07:07:13.073 に答える