一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

How to JSON decode array elements in JavaScript?

 gearss 2016-05-28

I have a JavaScript array that, among others, contains a URL. If I try to simply put the URL in the page (the array is in a project involving the Yahoo! Maps API) it shows the URL as it should be.

But if I try to do a redirect or simply do an 'alert' on the link array element I get:

function(){return JSON.encode(this);}

As far as I see it this is because the browser does an JSON.encode when it renders the page, thus the link is displayed OK. I have tried several methods to make it redirect (that's what I want to do with the link) correctly (including the usage of 'eval') but with no luck.

After following some suggestions I've run eval('(' + jsonObject + ')') but it still returns the same output.

So how's this done ?

asked Oct 8 '08 at 12:10
Brayn
75181932
    
It's not clear what you mean... can you post more sample code? The actual array contents and the redirect call would be very helpful. – Dylan Beattie Oct 8 '08 at 12:14
var obj = jQuery.parseJSON('{"name":"John"}');
alert( obj.name === "John" );

See the jQuery API.

answered Jun 30 '11 at 21:47

Suppose you have an array in PHP as $iniData with 5 fields. If using ajax -

echo json_encode($iniData);

In Javascript, use the following :

<script type="text/javascript">
    $(document).ready(function(){
        $.ajax({
            type: "GET",
            url: "ajaxCalls.php",
            data: "dataType=ini",
            success: function(msg)
            {
                var x = eval('(' + msg + ')');
                $('#allowed').html(x.allowed);              // these are the fields which you can now easily access..
                $('#completed').html(x.completed);
                $('#running').html(x.running);
                $('#expired').html(x.expired);
                $('#balance').html(x.balance);
            }
        });
    });
</script>
answered Mar 15 '11 at 7:38

If you get this text in an alert:

function(){return JSON.encode(this);}

when you try alert(myArray[i]), then there are a few possibilities:

  • myArray[i] is a function (most likely)
  • myArray[i] is the literal string "function(){return JSON.encode(this);}"
  • myArray[i] has a .toString() method that returns that function or that string. This is the least likely of the three.

The simplest way to tell would be to check typeof(myArray[i]).

answered Oct 8 '08 at 15:20
Joel Anair
10.3k31830
    
I doubt it that is a literal string. Most likely is a function. I have tried to "eval('(' + jsonObject + ')')" but still it returns the same output as before... – Brayn Oct 8 '08 at 15:58
    
Yeah, would imagine that passing a function to eval() would call that function's toString method, returning a string that would eval back to that function itself. So that makes sense. – Joel Anair Oct 8 '08 at 18:27
eval('(' + jsonObject + ')')
answered Oct 8 '08 at 15:23
Kon
19.2k74672
7  
eval is only a safe way to decode JSON data if you 100% trust your source :D – olliej Oct 8 '08 at 20:20
1  
This cant be eval-ed -> {"code":200,"lang":"en-de","text":["string 1","string 2"]} – Vova Popov Mar 29 '13 at 10:42

JSON decoding in JavaScript is simply an eval() if you trust the string or the more safe code you can find on http:// if you don't.

You will then have a JavaScript datastructure that you can traverse for the data you need.

answered Oct 8 '08 at 12:19
Lou Franco
59.8k989148
    
As I was saying I tried to 'eval' the link element but then it returned 'undefined'. The data in the array comes form parsing a XML, do you think that it can have something to do with that ? – Brayn Oct 8 '08 at 12:23

If the object element you get is a function, you can try this:

var url = myArray[i]();
answered Oct 9 '08 at 23:41
Leonel Martins
1,8251216

I decode JSON this way:

eval( 'var from_json_object = ' + my_json_str + ';' );
answered Mar 25 '13 at 11:33
pirogtm
262

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多

    国产又粗又猛又爽色噜噜| 国产亚洲午夜高清国产拍精品| 国产麻豆视频一二三区| 日韩欧美亚洲综合在线| 亚洲国产婷婷六月丁香| a久久天堂国产毛片精品| 欧美中文字幕日韩精品| 久久大香蕉一区二区三区| 日韩欧美国产高清在线| 亚洲香艳网久久五月婷婷| 老熟妇乱视频一区二区| 在线免费国产一区二区| 日韩精品视频香蕉视频| 欧美极品欧美精品欧美| 99久久免费看国产精品| 国产午夜精品亚洲精品国产| 国产免费一区二区三区av大片| 在线九月婷婷丁香伊人| 国产精品一区二区成人在线| 日本成人三级在线播放| 开心五月激情综合婷婷色| 日本熟妇五十一区二区三区| 国产精品午夜性色视频| 国产又粗又猛又爽色噜噜| 老司机精品福利视频在线播放| 成人精品国产亚洲av久久| 国产一区一一一区麻豆| 中文字幕一区二区三区中文| 欧美日韩一区二区三区色拉拉 | 东京热电东京热一区二区三区| 中文字幕欧美精品人妻一区| 亚洲欧美日韩色图七区| 激情五月天免费在线观看| 亚洲欧美国产精品一区二区| 亚洲高清亚洲欧美一区二区| 国产午夜精品久久福利| 在线免费看国产精品黄片| 热久久这里只有精品视频| 亚洲精品福利入口在线| 一级片黄色一区二区三区| 91香蕉视频精品在线看|