-2

次の JSON 文字列があります。

{
    "responseHeader":{
        "status":0,
        "QTime":2,
        "params":{
            "facet":"false",
            "fl":"id,title,friendlyurl,avatar,locpath,objectid,objecttype",
            "indent":"off",
            "q":"title_search:*castle*",
            "wt":"json",
            "fq":"userid:\"C325D42C-A777-4275-BDD2-D7810A8AB9AB\"",
            "defType":"lucene"
        }
    },
    "response":{
        "numFound":2,
        "start":0,
        "docs":[
            {
                "title":"castle a",
                "objecttype":1,
                "friendlyurl":"castle-a",
                "avatar":"6_887_castle-a.JPG",
                "objectid":6
            },
            {
                "title":"castle b",
                "objecttype":1,
                "friendlyurl":"castle-b",
                "avatar":"794_360_13j-castle-by-night.jpg",
                "objectid":794
            }
        ]
    }

}

ここには 2 件の検索結果があります: Castle A と Castle B

すべての結果をループして、属性 title、objecttype、および Friendlyurl の値を取得したい

テスト目的で、JSON文字列を変数「データ」に割り当てただけで、次のことを試しました。

    for (var i = 0, l = data.items.length; i < l; i++) {
        console.log(data.items[i].title);
        console.log(data.items[i].objecttype);
        console.log(data.items[i].friendlyurl);
    }

しかし、私は得る: Uncaught TypeError: Cannot read property 'length' of undefined

私が行った場合:

    data = $.parseJSON(data);
    for (var i = 0, l = data.items.length; i < l; i++) {
        console.log(data.items[i].title);
        console.log(data.items[i].objecttype);
        console.log(data.items[i].friendlyurl);
    }

私が得る: Uncaught SyntaxError: Unexpected token C jquery-1.8.3.min.js:2

js ファイルの行: e.JSON.parse(t);

私も試しました:

    var data = '{"responseHeader":{"status":0,"QTime":1,"params":{"facet":"false","fl":"id,title,friendlyurl,avatar,locpath,objectid,objecttype","indent":"off","start":"0","q":"title_search:*castle*","wt":"json","fq":"userid:\"C325D42C-A777-4275-BDD2-D7810A8AB9AB\"","rows":"10","defType":"lucene"}},"response":{"numFound":2,"start":0,"docs":[{"title":"castle a","objecttype":1,"friendlyurl":"castle-a","avatar":"6_887_castle-a.JPG","objectid":6},{"title":"castle b","objecttype":1,"friendlyurl":"castle-b","avatar":"794_360_13j-castle-by-night.jpg","objectid":794}]}}'

    var result,
          size = data.result.docs.length,
          index;
    for (index = 0; index < size; index++) {
        result = data.result.docs[index];
        console.log(result.title);
    }

しかし、それはエラーになります: Uncaught TypeError: Cannot read property 'docs' of undefined.

4

3 に答える 3

3

これは非常に基本的な JSON 処理です。このようなものから始めましょう:

data = $.parseJSON(json);
$.each(data.response.docs, function(i, item) {
    console.log(item.title);
    console.log(item.objecttype);
    console.log(item.friendlyurl);
});
于 2013-09-13T07:06:32.840 に答える
1

jQuery は必要ありません。このようなバニラ JavaScript を使用できます (この json が json という変数に割り当てられていると仮定します)。

var result,
      size = json.result.docs.length,
      index;
for(index=0; index<size;index++) {
  result = json.result.docs[index];
  console.log(result.title);
}
于 2013-09-13T07:08:38.443 に答える
0

すべての json オブジェクト プロパティをループして、必要なキー値を探す必要があります。基本的な操作は、呼び出しによって返されたオブジェクトをパラメーターとして持つ ajax 呼び出しで構成され、次に for each ループを使用して、必要なオブジェクト プロパティを確認できます。

これは、基本的な操作の外観です。

$.ajax({ 
    url: 'url_to_json.json',    
    dataType: 'json', // Choosing a JSON datatype
    success: function(data) // Variable data contains the data we get from JSON
    {           
        $.each(data, function(filterGroupKey, filterGroupValue){    
                if (filterGroupValue && filterGroupValue.hasOwnProperty('title')){
                    var filterTagModel = filterGroupValue;                  
                    filterTagModel.title =  filterGroupValue['title'];
                    filterTagModel.objectType =  filterGroupValue['objecttype'] ;
                    filterTagModel.friendlyUrl = filterGroupValue['friendlyurl'];
                }
            });        
    }
});

次に、filterTagModel とそのすべてのプロパティを使用して、取得したデータを操作できます。

于 2013-09-13T07:18:50.133 に答える