2017-01-25 13 views
0

json構造の要素にアクセスしようとしましたができません。私は以下を試した。名前の番号を持つJSON要素へのアクセス

私は、より大きなJSONオブジェクトの一部を抽出するためにnpmパッケージjson-queryを使用しています。これが動作しない

var rslt = jsonQuery('results[**][* status=active]', { 
        data: response 
       }); 

var make = JSON.stringify(rslt.value[0]["parms"]["make"]) 

Buは、私はまた、JSONから次のようなデータを取得することを試みた:

var img = JSON.stringify(rslt.value[0]["photos"]["1"]["320x240"]); 

var subtitle = JSON.stringify(rslt.value[0]["parms"]["price"]["1"]) + ' EUR'; 

これは私が働いているJSONデータ:

{ 
    "value":[ 
     { 
     "id":7038271775, 
     "user_id":1307227, 
     "status":"active", 
     "title":"", 
     "url":"https://www.autovit.ro/anunt/nissan-navara-ID7GjS0w.html", 
     "created_at":"2017-01-06 17:49:35", 
     "valid_to":"2017-02-03 17:56:16", 
     "description":"Data inmatriculare: 02.03.2015.\r\n4x4 \r\nABS \r\nAer conditionat \r\nComputer de bord \r\nGeamuri electrice \r\nInchidere centralizata \r\nJante aliaj \r\nRadio CD \r\nServodirectie \r\nVolan multifunctional", 
     "category_id":29, 
     "region_id":46, 
     "city_id":24691, 
     "city":{ 
      "ro":"Voluntari", 
      "en":"Voluntari" 
     }, 
     "coordinates":{ 
      "latitude":44.49192877, 
      "longitude":26.12458706, 
      "radius":0, 
      "zoom_level":16 
     }, 
     "advertiser_type":"business", 
     "contact":{ 
      "person":"LeasePlan Outlet Center", 
      "phone_numbers":[ 
       "0753312151" 
      ] 
     }, 
     "params":{ 
      "make":"nissan", 
      "model":"navara", 
      "year":2014, 
      "mileage":16785, 
      "engine_capacity":2500, 
      "vin":"VSKCVND40U0566467", 
      "fuel_type":"diesel", 
      "gearbox":"manual", 
      "transmission":"all-wheel-lock", 
      "particle_filter":"0", 
      "green_tax":"1", 
      "damaged":"0", 
      "body_type":"suv", 
      "door_count":4, 
      "color":"white", 
      "metallic":"0", 
      "pearl":"0", 
      "matt":"0", 
      "rhd":"0", 
      "features":[ ], 
      "price":{ 
       "0":"price", 
       "1":15900, 
       "currency":"EUR", 
       "gross_net":"net" 
      }, 
      "vat":"1", 
      "financial_option":"1", 
      "leasing_concession":"0", 
      "date_registration":"2014-1-01", 
      "registered":"1", 
      "original_owner":"1", 
      "no_accident":"0", 
      "service_record":"0", 
      "historical_vehicle":"0", 
      "tuning":"0" 
     }, 
     "photos":{ 
      "1":{ 
       "1080x720":"https://img41.autovit.ro/images_autovitro/820558483_1_1080x720.jpg", 
       "732x488":"https://img40.autovit.ro/images_autovitro/820558483_1_732x488.jpg", 
       "320x240":"https://img42.autovit.ro/images_autovitro/820558483_1_320x240.jpg", 
       "148x110":"https://img40.autovit.ro/images_autovitro/820558483_1_148x110.jpg" 
      }, 
      "2":{ 
       "1080x720":"https://img41.autovit.ro/images_autovitro/820558483_2_1080x720.jpg", 
       "732x488":"https://img42.autovit.ro/images_autovitro/820558483_2_732x488.jpg", 
       "320x240":"https://img42.autovit.ro/images_autovitro/820558483_2_320x240.jpg", 
       "148x110":"https://img42.autovit.ro/images_autovitro/820558483_2_148x110.jpg" 
      }, 
      "3":{ 
       "1080x720":"https://img42.autovit.ro/images_autovitro/820558483_3_1080x720.jpg", 
       "732x488":"https://img42.autovit.ro/images_autovitro/820558483_3_732x488.jpg", 
       "320x240":"https://img40.autovit.ro/images_autovitro/820558483_3_320x240.jpg", 
       "148x110":"https://img42.autovit.ro/images_autovitro/820558483_3_148x110.jpg" 
      }, 
      "4":{ 
       "1080x720":"https://img42.autovit.ro/images_autovitro/820558483_4_1080x720.jpg", 
       "732x488":"https://img40.autovit.ro/images_autovitro/820558483_4_732x488.jpg", 
       "320x240":"https://img42.autovit.ro/images_autovitro/820558483_4_320x240.jpg", 
       "148x110":"https://img42.autovit.ro/images_autovitro/820558483_4_148x110.jpg" 
      }, 
      "5":{ 
       "1080x720":"https://img41.autovit.ro/images_autovitro/820558483_5_1080x720.jpg", 
       "732x488":"https://img41.autovit.ro/images_autovitro/820558483_5_732x488.jpg", 
       "320x240":"https://img41.autovit.ro/images_autovitro/820558483_5_320x240.jpg", 
       "148x110":"https://img40.autovit.ro/images_autovitro/820558483_5_148x110.jpg" 
      }, 
      "6":{ 
       "1080x720":"https://img40.autovit.ro/images_autovitro/820558483_6_1080x720.jpg", 
       "732x488":"https://img42.autovit.ro/images_autovitro/820558483_6_732x488.jpg", 
       "320x240":"https://img40.autovit.ro/images_autovitro/820558483_6_320x240.jpg", 
       "148x110":"https://img42.autovit.ro/images_autovitro/820558483_6_148x110.jpg" 
      }, 
      "7":{ 
       "1080x720":"https://img40.autovit.ro/images_autovitro/820558483_7_1080x720.jpg", 
       "732x488":"https://img41.autovit.ro/images_autovitro/820558483_7_732x488.jpg", 
       "320x240":"https://img40.autovit.ro/images_autovitro/820558483_7_320x240.jpg", 
       "148x110":"https://img40.autovit.ro/images_autovitro/820558483_7_148x110.jpg" 
      }, 
      "8":{ 
       "1080x720":"https://img42.autovit.ro/images_autovitro/820558483_8_1080x720.jpg", 
       "732x488":"https://img41.autovit.ro/images_autovitro/820558483_8_732x488.jpg", 
       "320x240":"https://img42.autovit.ro/images_autovitro/820558483_8_320x240.jpg", 
       "148x110":"https://img40.autovit.ro/images_autovitro/820558483_8_148x110.jpg" 
      }, 
      "9":{ 
       "1080x720":"https://img41.autovit.ro/images_autovitro/820558483_9_1080x720.jpg", 
       "732x488":"https://img40.autovit.ro/images_autovitro/820558483_9_732x488.jpg", 
       "320x240":"https://img40.autovit.ro/images_autovitro/820558483_9_320x240.jpg", 
       "148x110":"https://img42.autovit.ro/images_autovitro/820558483_9_148x110.jpg" 
      }, 
      "10":{ 
       "1080x720":"https://img41.autovit.ro/images_autovitro/820558483_10_1080x720.jpg", 
       "732x488":"https://img42.autovit.ro/images_autovitro/820558483_10_732x488.jpg", 
       "320x240":"https://img40.autovit.ro/images_autovitro/820558483_10_320x240.jpg", 
       "148x110":"https://img40.autovit.ro/images_autovitro/820558483_10_148x110.jpg" 
      }, 
      "11":{ 
       "1080x720":"https://img42.autovit.ro/images_autovitro/820558483_11_1080x720.jpg", 
       "732x488":"https://img42.autovit.ro/images_autovitro/820558483_11_732x488.jpg", 
       "320x240":"https://img41.autovit.ro/images_autovitro/820558483_11_320x240.jpg", 
       "148x110":"https://img42.autovit.ro/images_autovitro/820558483_11_148x110.jpg" 
      } 
     }, 
     "image_collection_id":820558483, 
     "last_update_date":"2017-01-24 18:00:26", 
     "new_used":"used" 
     } 
    ], 
    "is_last_page":false, 
    "is_first_page":true, 
    "current_page":1, 
    "total_pages":33, 
    "current_elements":1, 
    "total_elements":33 
} 
+0

それはJavaScriptでオブジェクトリテラルなら、それは(実際には架空のものである)JSONオブジェクトではありません。 ** JavaScript **オブジェクトです。コードにはどのように表示されますか? '' result ''はオブジェクトの**プロパティ**のように見えます;最初にオブジェクト自体への参照が必要であるということにアクセスするには、コードから何が得られたのかは不明です。 – Pointy

+0

JSONオブジェクトがあり、JSON.stringify(rslt.value [0] ["photos"] ["1"] ["320x240"]);次の作業を行う方法がわかりません。 1と320x240を角かっこで囲む必要がありますか? – user1907509

+0

var img = JSON.stringify(rslt.value [0] .photos.1.320x240)を使用しようとしました。これはうまくいきませんでした – user1907509

答えて

1

私はあなたのコードで表示される唯一の問題は、あなたが書いて間違っているとあなたが書いてparamsとあなたはparms

enter image description here

2

私はexcatly「JSON-クエリは、」何をするかわかりませんが、私はあなたが安全に、このように任意のJSONの一部を抽出することができると思います。

function dig(input, key) { 
    var keys = (""+ key).split("."), key = keys.shift(); 
    if (!keys.length) { 
     return input[key]; 
    } 
    return dig(input[key], keys.join(".")); 
} 

// here that gives me already without any error thrown >> https://img42.autovit.ro/images_autovitro/820558483_1_320x240.jpg 
var p = dig(rslt, "results.0.photos.1.320x240"); 
console.log(p); 

// this is same with 
console.log(rslt.results[0].photos[1]["320x240"]); 

クレジット:https://github.com/yay-couch/couch-js/blob/master/couch/util/util.js#L46

関連する問題