如何使用jquery打印json数据

时间:2017-07-25 09:08:12

标签: jquery json

我有

的json数据
     <script> 
    // wait for the DOM to be loaded 
    $(document).ready(function() { 
        // bind 'myForm' and provide a simple callback function 
        $('#myForm').ajaxForm(function(data) { 

            alert(data);


        }); 
    }); 
</script>

我可以打印所有数据,但我想打印像

这样的键值
   alert(data->regrinfo);
   alert(data->name);

我的json数据

{
  "regrinfo": {
    "domain": {
      "name": "dolarbey.com",
      "nserver": {
        "ns1.pixeldns.net": "159.253.42.100",
        "ns2.pixeldns.net": "94.102.11.86"
      },
      "status": "clientTransferProhibited https://icann.org/epp#clientTransferProhibited",
      "changed": "2016-12-07",
      "created": "2016-07-12",
      "expires": "2017-07-12",
      "sponsor": "PIXELNET HOSTING"
    },
    "registered": "yes"
  },
  "regyinfo": {
    "registrar": "PDR LTD. D/B/A PUBLICDOMAINREGISTRY.COM",
    "referrer": "http://www.publicdomainregistry.com",
    "servers": [
      {
        "server": "whois.verisign-grs.com",
        "args": "dolarbey.com",
        "port": 43
      },
      {
        "server": "whois.PublicDomainRegistry.com",
        "args": "dolarbey.com",
        "port": 43
      }
    ],
    "type": "domain"
  },
  "rawdata": [
    "Domain Name: DOLARBEY.COM",
    "Registry Domain ID: 2079883307_DOMAIN_COM-VRSN",
    "Registrar WHOIS Server: whois.publicdomainregistry.com",
    "Registrar URL: www.publicdomainregistry.com",
    "Updated Date: 2017-02-06T02:27:08Z",
    "Creation Date: 2016-12-07T17:49:49Z",
    "Registrar Registration Expiration Date: 2017-12-07T17:49:49Z",
    "Registrar: PDR Ltd. d/b/a PublicDomainRegistry.com",
    "Registrar IANA ID: 303",
    "Domain Status: clientTransferProhibited https://icann.org/epp#clientTransferProhibited",
    "Registry Registrant ID: Not Available From Registry",
    "Registrant Name: Muhammed ERDEM",
    "Registrant Organization: ",
    "Registrant Street: asdlkjasd a\u015flsdkjaslkd askd asdasd ",
    "Registrant City: diyarbak\u0131r",
    "Registrant State/Province: diyarbak\u0131r",
    "Registrant Postal Code: 21500",
    "Registrant Country: TR",
    "Registrant Phone: +90.5070473099",
    "Registrant Phone Ext: ",
    "Registrant Fax: ",
    "Registrant Fax Ext: ",
    "Registrant Email: dolarbey21@gmail.com",
    "Registry Admin ID: Not Available From Registry",
    "Admin Name: Muhammed ERDEM",
    "Admin Organization: ",
    "Admin Street: asdlkjasd a\u015flsdkjaslkd askd asdasd ",
    "Admin City: diyarbak\u0131r",
    "Admin State/Province: diyarbak\u0131r",
    "Admin Postal Code: 21500",
    "Admin Country: TR",
    "Admin Phone: +90.5070473099",
    "Admin Phone Ext: ",
    "Admin Fax: ",
    "Admin Fax Ext: ",
    "Admin Email: dolarbey21@gmail.com",
    "Registry Tech ID: Not Available From Registry",
    "Tech Name: Muhammed ERDEM",
    "Tech Organization: ",
    "Tech Street: asdlkjasd a\u015flsdkjaslkd askd asdasd ",
    "Tech City: diyarbak\u0131r",
    "Tech State/Province: diyarbak\u0131r",
    "Tech Postal Code: 21500",
    "Tech Country: TR",
    "Tech Phone: +90.5070473099",
    "Tech Phone Ext: ",
    "Tech Fax: ",
    "Tech Fax Ext: ",
    "Tech Email: dolarbey21@gmail.com",
    "Name Server: ns1.pixeldns.net",
    "Name Server: ns2.pixeldns.net",
    "DNSSEC:Unsigned",
    "Registrar Abuse Contact Email: abuse-contact@publicdomainregistry.com",
    "Registrar Abuse Contact Phone: +1.2013775952",
    "URL of the ICANN WHOIS Data Problem Reporting System: http://wdprs.internic.net/",
    ">>> Last update of WHOIS database: 2017-07-25T09:00:58Z <<<",
    "",
    "For more information on Whois status codes, please visit https://icann.org/epp",
    "",
    "Registration Service Provided By: PIXELNET HOSTING",
    "",
    "The data in this whois database is provided to you for information purposes ",
    "only, that is, to assist you in obtaining information about or related to a ",
    "domain name registration record. We make this information available \"as is\",",
    "and do not guarantee its accuracy. By submitting a whois query, you agree ",
    "that you will use this data only for lawful purposes and that, under no ",
    "circumstances will you use this data to: ",
    "(1) enable high volume, automated, electronic processes that stress or load ",
    "this whois database system providing you this information; or ",
    "(2) allow, enable, or otherwise support the transmission of mass unsolicited, ",
    "commercial advertising or solicitations via direct mail, electronic mail, or ",
    "by telephone. ",
    "The compilation, repackaging, dissemination or other use of this data is ",
    "expressly prohibited without prior written consent from us. The Registrar of ",
    "record is PDR Ltd. d/b/a PublicDomainRegistry.com. ",
    "We reserve the right to modify these terms at any time. ",
    "By submitting this query, you agree to abide by these terms.",
    "",
    ""
  ]
}

3 个答案:

答案 0 :(得分:1)

  1. 使用.each()循环访问数据。 .each()可以有2个参数用于索引,1个用于值,您可以获得索引和值匹配
  2. &#13;
    &#13;
    var data = {
    	"regrinfo": {
    		"domain": {
    			"name": "dolarbey.com",
    			"nserver": {
    				"ns1.pixeldns.net": "159.253.42.100",
    				"ns2.pixeldns.net": "94.102.11.86"
    			},
    			"status": "clientTransferProhibited https://icann.org/epp#clientTransferProhibited",
    			"changed": "2016-12-07",
    			"created": "2016-07-12",
    			"expires": "2017-07-12",
    			"sponsor": "PIXELNET HOSTING"
    		},
    		"registered": "yes"
    	},
    	"regyinfo": {
    		"registrar": "PDR LTD. D/B/A PUBLICDOMAINREGISTRY.COM",
    		"referrer": "http://www.publicdomainregistry.com",
    		"servers": [{
    			"server": "whois.verisign-grs.com",
    			"args": "dolarbey.com",
    			"port": 43
    		}, {
    			"server": "whois.PublicDomainRegistry.com",
    			"args": "dolarbey.com",
    			"port": 43
    		}],
    		"type": "domain"
    	},
    	"rawdata": ["Domain Name: DOLARBEY.COM", "Registry Domain ID: 2079883307_DOMAIN_COM-VRSN", "Registrar WHOIS Server: whois.publicdomainregistry.com", "Registrar URL: www.publicdomainregistry.com", "Updated Date: 2017-02-06T02:27:08Z", "Creation Date: 2016-12-07T17:49:49Z", "Registrar Registration Expiration Date: 2017-12-07T17:49:49Z", "Registrar: PDR Ltd. d/b/a PublicDomainRegistry.com", "Registrar IANA ID: 303", "Domain Status: clientTransferProhibited https://icann.org/epp#clientTransferProhibited", "Registry Registrant ID: Not Available From Registry", "Registrant Name: Muhammed ERDEM", "Registrant Organization: ", "Registrant Street: asdlkjasd a\u015flsdkjaslkd askd asdasd ", "Registrant City: diyarbak\u0131r", "Registrant State/Province: diyarbak\u0131r", "Registrant Postal Code: 21500", "Registrant Country: TR", "Registrant Phone: +90.5070473099", "Registrant Phone Ext: ", "Registrant Fax: ", "Registrant Fax Ext: ", "Registrant Email: dolarbey21@gmail.com", "Registry Admin ID: Not Available From Registry", "Admin Name: Muhammed ERDEM", "Admin Organization: ", "Admin Street: asdlkjasd a\u015flsdkjaslkd askd asdasd ", "Admin City: diyarbak\u0131r", "Admin State/Province: diyarbak\u0131r", "Admin Postal Code: 21500", "Admin Country: TR", "Admin Phone: +90.5070473099", "Admin Phone Ext: ", "Admin Fax: ", "Admin Fax Ext: ", "Admin Email: dolarbey21@gmail.com", "Registry Tech ID: Not Available From Registry", "Tech Name: Muhammed ERDEM", "Tech Organization: ", "Tech Street: asdlkjasd a\u015flsdkjaslkd askd asdasd ", "Tech City: diyarbak\u0131r", "Tech State/Province: diyarbak\u0131r", "Tech Postal Code: 21500", "Tech Country: TR", "Tech Phone: +90.5070473099", "Tech Phone Ext: ", "Tech Fax: ", "Tech Fax Ext: ", "Tech Email: dolarbey21@gmail.com", "Name Server: ns1.pixeldns.net", "Name Server: ns2.pixeldns.net", "DNSSEC:Unsigned", "Registrar Abuse Contact Email: abuse-contact@publicdomainregistry.com", "Registrar Abuse Contact Phone: +1.2013775952", "URL of the ICANN WHOIS Data Problem Reporting System: http://wdprs.internic.net/", ">>> Last update of WHOIS database: 2017-07-25T09:00:58Z <<<", "", "For more information on Whois status codes, please visit https://icann.org/epp", "", "Registration Service Provided By: PIXELNET HOSTING", "", "The data in this whois database is provided to you for information purposes ", "only, that is, to assist you in obtaining information about or related to a ", "domain name registration record. We make this information available \"as is\",", "and do not guarantee its accuracy. By submitting a whois query, you agree ", "that you will use this data only for lawful purposes and that, under no ", "circumstances will you use this data to: ", "(1) enable high volume, automated, electronic processes that stress or load ", "this whois database system providing you this information; or ", "(2) allow, enable, or otherwise support the transmission of mass unsolicited, ", "commercial advertising or solicitations via direct mail, electronic mail, or ", "by telephone. ", "The compilation, repackaging, dissemination or other use of this data is ", "expressly prohibited without prior written consent from us. The Registrar of ", "record is PDR Ltd. d/b/a PublicDomainRegistry.com. ", "We reserve the right to modify these terms at any time. ", "By submitting this query, you agree to abide by these terms.", "", ""]
    }
    
    
    
    $.each(data,function(i,v){
    console.log("key is " + i)
    console.log("value is " + v)
    
    })
    &#13;
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    &#13;
    &#13;
    &#13;

答案 1 :(得分:0)

对数据使用JSON.parse,然后您就可以访问各个值。同时使用dotbrackets表示data.namedata["name"];

的访问对象值
$(document).ready(function() { 
    // bind 'myForm' and provide a simple callback function 
    $('#myForm').ajaxForm(function(data) { 
        var json = JSON.parse(data);
        alert(data.name);


    }); 
}); 

答案 2 :(得分:0)

<script> 
    // wait for the DOM to be loaded 
    $(document).ready(function() { 
        // bind 'myForm' and provide a simple callback function 
        $('#myForm').ajaxForm(function(data) { 
            for(var key in data) {
                alert(key+":"+data[key]);
            }
        }); 
    }); 
</script>