现在有一个json格式的数据是从api那里获取的(所以不能修改)。
{ "message":[{"cndn.net":"unavailable"},{"cndn.com":"unavailable"},{"cndn.biz":"available"}]}  现在我需要的是将message集合里的cndn.net cndn.com cndn.biz的值最好有用循环输出,我的需求是这样了JSONAPI美橙接口

解决方案 »

  1.   


    var json='{ "message":[{"cndn.net":"unavailable"},{"cndn.com":"unavailable"},{"cndn.biz":"available"}]}';
    eval('var o='+json);
      o=o.message;
    for(var i=0;i<o.length;i++){
     for(k in o[i]) alert(k)
    }
      

  2.   

    var json='{ "message":[{"cndn.net":"unavailable"},{"cndn.com":"unavailable"},{"cndn.biz":"available"}]}';
    eval('var o='+json);
      o=o.message;
    for(var i=0;i<o.length;i++){
         for(k in o[i]) alert([k,o[i][k]])
    }
      

  3.   


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    </head><body>
    <script type="text/javascript">var json='{"message":[{"cndn.net":"unavailable"},{"cndn.com":"unavailable"},{"cndn.biz":"available"}]}';
    var o=JSON.parse(json);
    for(i in o.message){
     for(k in o.message[i]){
      alert(k)
     }
    }</script>
    </body>
    </html>
      

  4.   

    如果没用jquery 就直接
    var o=eval("("+json+")");  也可行。
      

  5.   

    不要eval,那个是有风险的,你宁可自己用split拆,也可以导入Ext的json类或者jquery进行处理
      

  6.   

    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>jQuery.extend demo</title>
      <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    </head>
    <body>
     
    <div id="log"></div>
     
    <script>
    var object1 = {
      apple: 0,
      banana: {weight: 52, price: 100},
      cherry: 97
    };
    var object2 = {
      banana: {price: 200},
      durian: 100
    };
     
    /* merge object2 into object1 */
    $.extend(object1, object2);
     
    var printObj = typeof JSON != "undefined" ? JSON.stringify : function(obj) {
      var arr = [];
      $.each(obj, function(key, val) {
        var next = key + ": ";
        next += $.isPlainObject(val) ? printObj(val) : val;
        arr.push( next );
      });
      return "{ " +  arr.join(", ") + " }";
    };
     
    $("#log").append( printObj(object1) );
    </script>
     
    </body>
    </html>
      

  7.   

    呵呵~谢谢大家了~这几天出差了忘记带上电脑~现在把分给上你们吧~我这里有我自己的答案~~思路是采用u011461314的其他人我也象征性给分吧~谢谢你们哦~
    function OutputHtml(){
    var html = "";
    //循环查找对象 
    for(var i = 0;i<pjn.length;i++){
    for(k in pjn[i])

       
    html += "<div id='jsonmeta'>";
    html +="<ul style='margin-top:8px;list-style:none outside none;'>";
    html +="<li class='json_1'>" if(pjn[i][k]=="unavailable"){
    html +="<img width='12' height='12' src='http://www.cantect.cn/images/domain_4.jpg'></li>"
    html +="<li class='json_2'>"+k+"<span class='ybzc'>已被注册</span></li>"
    html +="<li class='json_3'><a href='http://who.cndns.com/?d="+k+"' target='_blank'>查看whois信息</a></li>"
    }
    else
    {
    html +="<img width='12' height='12' src='http://www.cantect.cn/images/domain_3.jpg'></li>"
    html +="<li class='json_2'>"+k+"<span class='swzc'>尚未注册</span></li>"
    html +="<li class='json_3'><a href='http://www.cantect.cn/company_culture/connect_us/index.jsp' target='_blank'>联系我们</a></li>"
    }
    html +="</ul></div>"

    //html += "<a href='" +sites[i].templatetestURL+ "' rel='book' title=" +sites[i].templatename+ ">";
    //html += "<img src='" +sites[i].templateIMGURL+ "' width='200' height='170' />";
    //html += "<p class='url'><span>" +sites[i].templatename+ "</span></p></a>";
    //html += "</div>";


    }
    $("json").innerHTML = html;
    }
      

  8.   

    不好意思~思路是采用fzfei2,有粘上代码的我都给1分吧~其他都归fzfei2