代码之家  ›  专栏  ›  技术社区  ›  Programer Beginner

使用JQuery处理JSON数据

  •  -1
  • Programer Beginner  · 技术社区  · 8 年前

    这是我的json数据(也可用 here )以下内容:

    [{"id":{"$t":"https://spreadsheets.google.com/feeds/list/12fmjZfRgJHlSulaWQdmvaz1yinmwaTwsdHsOzRfYNBE/od6/public/values/cokwr"},"updated":{"$t":"2018-06-23T22:12:49.197Z"},"category":[{"scheme":"http://schemas.google.com/spreadsheets/2006","term":"http://schemas.google.com/spreadsheets/2006#list"}],"title":{"type":"text","$t":"Row: 2"},"content":{"type":"text","$t":"_cokwr: Heading 1"},"link":[{"rel":"self","type":"application/atom+xml","href":"https://spreadsheets.google.com/feeds/list/12fmjZfRgJHlSulaWQdmvaz1yinmwaTwsdHsOzRfYNBE/od6/public/values/cokwr"}],"gsx$_cokwr":{"$t":"Heading 1"}},{"id":{"$t":"https://spreadsheets.google.com/feeds/list/12fmjZfRgJHlSulaWQdmvaz1yinmwaTwsdHsOzRfYNBE/od6/public/values/cpzh4"},"updated":{"$t":"2018-06-23T22:12:49.197Z"},"category":[{"scheme":"http://schemas.google.com/spreadsheets/2006","term":"http://schemas.google.com/spreadsheets/2006#list"}],"title":{"type":"text","$t":"Row: 3"},"content":{"type":"text","$t":"_cokwr: John, _cpzh4: $9.00"},"link":[{"rel":"self","type":"application/atom+xml","href":"https://spreadsheets.google.com/feeds/list/12fmjZfRgJHlSulaWQdmvaz1yinmwaTwsdHsOzRfYNBE/od6/public/values/cpzh4"}],"gsx$_cokwr":{"$t":"John"},"gsx$_cpzh4":{"$t":"$9.00"}},{"id":{"$t":"https://spreadsheets.google.com/feeds/list/12fmjZfRgJHlSulaWQdmvaz1yinmwaTwsdHsOzRfYNBE/od6/public/values/cre1l"},"updated":{"$t":"2018-06-23T22:12:49.197Z"},"category":[{"scheme":"http://schemas.google.com/spreadsheets/2006","term":"http://schemas.google.com/spreadsheets/2006#list"}],"title":{"type":"text","$t":"Row: 4"},"content":{"type":"text","$t":"_cokwr: Elijah, _cpzh4: $12.00"},"link":[{"rel":"self","type":"application/atom+xml","href":"https://spreadsheets.google.com/feeds/list/12fmjZfRgJHlSulaWQdmvaz1yinmwaTwsdHsOzRfYNBE/od6/public/values/cre1l"}],"gsx$_cokwr":{"$t":"Elijah"},"gsx$_cpzh4":{"$t":"$12.00"}},{"id":{"$t":"https://spreadsheets.google.com/feeds/list/12fmjZfRgJHlSulaWQdmvaz1yinmwaTwsdHsOzRfYNBE/od6/public/values/chk2m"},"updated":{"$t":"2018-06-23T22:12:49.197Z"},"category":[{"scheme":"http://schemas.google.com/spreadsheets/2006","term":"http://schemas.google.com/spreadsheets/2006#list"}],"title":{"type":"text","$t":"Row: 5"},"content":{"type":"text","$t":"_cokwr: Andrew, _cpzh4: $1.00"},"link":[{"rel":"self","type":"application/atom+xml","href":"https://spreadsheets.google.com/feeds/list/12fmjZfRgJHlSulaWQdmvaz1yinmwaTwsdHsOzRfYNBE/od6/public/values/chk2m"}],"gsx$_cokwr":{"$t":"Andrew"},"gsx$_cpzh4":{"$t":"$1.00"}}]
    

    它基本上是一个json格式的google电子表格。

    我想要的是钱(从$开始)。在谷歌的表格中,它只出现在C列,特别是现在我试图得到“12.00美元”(手机: 补体第四成份 )

    我试过了 $(".results").prepend('<b>'+data.feed.entry.gsx$_cpzh4.$t+'</b>'); 得到:

    uncaught typeerror:无法读取未定义的属性'$t'

    注意:请保持您的答案简单,因为我刚刚开始学习JavaScript和jQuery 12小时前。

    1 回复  |  直到 8 年前
        1
  •  1
  •   Muhammad Omer Aslam    8 年前

    一些事情

    • 首先 entry 是一个数组。

    • 以及你试图访问的属性 gsx$_cpzh4 不存在于内部的所有对象中 进入 阵列。

    您需要使用简单的 forEach 并访问所有具有通过使用 .hasOwnProperty() ,下面提供了一个简单的控制台打印演示 json 你可以看到它用 entry.gsx$_cpzh4.$t 对于 进入 数组并跳过未定义该属性的数组。

    //iterate over al lthe entries
    data.feed.entry.forEach(function(entry) {
      //if it has the property defined with name gsx$_cpzh4
      if (entry.hasOwnProperty('gsx$_cpzh4')) {
        //print the prices
        console.log(entry['gsx$_cpzh4'].$t);
      }
    });
    

    let data = {
      "version": "1.0",
      "encoding": "UTF-8",
      "feed": {
        "xmlns": "http://www.w3.org/2005/Atom",
        "xmlns$openSearch": "http://a9.com/-/spec/opensearchrss/1.0/",
        "xmlns$gsx": "http://schemas.google.com/spreadsheets/2006/extended",
        "id": {
          "$t": "https://spreadsheets.google.com/feeds/list/12fmjZfRgJHlSulaWQdmvaz1yinmwaTwsdHsOzRfYNBE/od6/public/values"
        },
        "updated": {
          "$t": "2018-06-23T22:12:49.197Z"
        },
        "category": [{
          "scheme": "http://schemas.google.com/spreadsheets/2006",
          "term": "http://schemas.google.com/spreadsheets/2006#list"
        }],
        "title": {
          "type": "text",
          "$t": "Sheet1"
        },
        "link": [{
            "rel": "alternate",
            "type": "application/atom+xml",
            "href": "https://docs.google.com/spreadsheets/d/12fmjZfRgJHlSulaWQdmvaz1yinmwaTwsdHsOzRfYNBE/pubhtml"
          },
          {
            "rel": "http://schemas.google.com/g/2005#feed",
            "type": "application/atom+xml",
            "href": "https://spreadsheets.google.com/feeds/list/12fmjZfRgJHlSulaWQdmvaz1yinmwaTwsdHsOzRfYNBE/od6/public/values"
          },
          {
            "rel": "http://schemas.google.com/g/2005#post",
            "type": "application/atom+xml",
            "href": "https://spreadsheets.google.com/feeds/list/12fmjZfRgJHlSulaWQdmvaz1yinmwaTwsdHsOzRfYNBE/od6/public/values"
          },
          {
            "rel": "self",
            "type": "application/atom+xml",
            "href": "https://spreadsheets.google.com/feeds/list/12fmjZfRgJHlSulaWQdmvaz1yinmwaTwsdHsOzRfYNBE/od6/public/values?alt=json"
          }
        ],
        "author": [{
          "name": {
            "$t": "gem7008"
          },
          "email": {
            "$t": "gem7008@gmail.com"
          }
        }],
        "openSearch$totalResults": {
          "$t": "4"
        },
        "openSearch$startIndex": {
          "$t": "1"
        },
        "entry": [{
            "id": {
              "$t": "https://spreadsheets.google.com/feeds/list/12fmjZfRgJHlSulaWQdmvaz1yinmwaTwsdHsOzRfYNBE/od6/public/values/cokwr"
            },
            "updated": {
              "$t": "2018-06-23T22:12:49.197Z"
            },
            "category": [{
              "scheme": "http://schemas.google.com/spreadsheets/2006",
              "term": "http://schemas.google.com/spreadsheets/2006#list"
            }],
            "title": {
              "type": "text",
              "$t": "Row: 2"
            },
            "content": {
              "type": "text",
              "$t": "_cokwr: Heading 1"
            },
            "link": [{
              "rel": "self",
              "type": "application/atom+xml",
              "href": "https://spreadsheets.google.com/feeds/list/12fmjZfRgJHlSulaWQdmvaz1yinmwaTwsdHsOzRfYNBE/od6/public/values/cokwr"
            }],
            "gsx$_cokwr": {
              "$t": "Heading 1"
            }
          },
          {
            "id": {
              "$t": "https://spreadsheets.google.com/feeds/list/12fmjZfRgJHlSulaWQdmvaz1yinmwaTwsdHsOzRfYNBE/od6/public/values/cpzh4"
            },
            "updated": {
              "$t": "2018-06-23T22:12:49.197Z"
            },
            "category": [{
              "scheme": "http://schemas.google.com/spreadsheets/2006",
              "term": "http://schemas.google.com/spreadsheets/2006#list"
            }],
            "title": {
              "type": "text",
              "$t": "Row: 3"
            },
            "content": {
              "type": "text",
              "$t": "_cokwr: John, _cpzh4: $9.00"
            },
            "link": [{
              "rel": "self",
              "type": "application/atom+xml",
              "href": "https://spreadsheets.google.com/feeds/list/12fmjZfRgJHlSulaWQdmvaz1yinmwaTwsdHsOzRfYNBE/od6/public/values/cpzh4"
            }],
            "gsx$_cokwr": {
              "$t": "John"
            },
            "gsx$_cpzh4": {
              "$t": "$9.00"
            }
          },
          {
            "id": {
              "$t": "https://spreadsheets.google.com/feeds/list/12fmjZfRgJHlSulaWQdmvaz1yinmwaTwsdHsOzRfYNBE/od6/public/values/cre1l"
            },
            "updated": {
              "$t": "2018-06-23T22:12:49.197Z"
            },
            "category": [{
              "scheme": "http://schemas.google.com/spreadsheets/2006",
              "term": "http://schemas.google.com/spreadsheets/2006#list"
            }],
            "title": {
              "type": "text",
              "$t": "Row: 4"
            },
            "content": {
              "type": "text",
              "$t": "_cokwr: Elijah, _cpzh4: $12.00"
            },
            "link": [{
              "rel": "self",
              "type": "application/atom+xml",
              "href": "https://spreadsheets.google.com/feeds/list/12fmjZfRgJHlSulaWQdmvaz1yinmwaTwsdHsOzRfYNBE/od6/public/values/cre1l"
            }],
            "gsx$_cokwr": {
              "$t": "Elijah"
            },
            "gsx$_cpzh4": {
              "$t": "$12.00"
            }
          },
          {
            "id": {
              "$t": "https://spreadsheets.google.com/feeds/list/12fmjZfRgJHlSulaWQdmvaz1yinmwaTwsdHsOzRfYNBE/od6/public/values/chk2m"
            },
            "updated": {
              "$t": "2018-06-23T22:12:49.197Z"
            },
            "category": [{
              "scheme": "http://schemas.google.com/spreadsheets/2006",
              "term": "http://schemas.google.com/spreadsheets/2006#list"
            }],
            "title": {
              "type": "text",
              "$t": "Row: 5"
            },
            "content": {
              "type": "text",
              "$t": "_cokwr: Andrew, _cpzh4: $1.00"
            },
            "link": [{
              "rel": "self",
              "type": "application/atom+xml",
              "href": "https://spreadsheets.google.com/feeds/list/12fmjZfRgJHlSulaWQdmvaz1yinmwaTwsdHsOzRfYNBE/od6/public/values/chk2m"
            }],
            "gsx$_cokwr": {
              "$t": "Andrew"
            },
            "gsx$_cpzh4": {
              "$t": "$1.00"
            }
          }
        ]
      }
    }
    
    data.feed.entry.forEach(function(entry) {
      if (entry.hasOwnProperty('gsx$_cpzh4')) {
        console.log(entry['gsx$_cpzh4'].$t);
        $(".results").prepend('<b>' + entry['gsx$_cpzh4'].$t + '</b>')
      }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="results">
    
    </div>