簡體   English   中英

通過變量讀取和解析JSON

[英]Read and parse JSON by variable

我有一個值存儲為鍵值。 在這種情況下,例如“ 12F”。 我想看看它是否存在於我的JSON中。 如果是這樣,我想獲取PNG值。 如果沒有,請發回一條消息

JSON

{
    "Property": "99",
    "12F": {
        "png": "12-74"
    },
    "13F": {
        "png": "12-74"
    }
}

JQUERY

var sourceurl = '../floorplan-data.json';
        var thekeyvalue = $("#finalkey").text();
        //start ajax request
        $.ajax({
            url: sourceurl,
            //force to handle it as text
            dataType: "text",
            error: 
           function(){
              //error
           },
            success: function(data) {
              var json = $.parseJSON(data);
              console.log(json.thekeyvalue); //having trouble here
            }
        });

這就是您的成功職能應該如何。 請檢查。

   var sourceurl = '../floorplan-data.json';
    var thekeyvalue = $("#finalkey").text();
    //start ajax request
    $.ajax({
        url: sourceurl,
        //force to handle it as text
        dataType: "text",
        error: 
       function(){
          //error
       },
        success: function(data) {
          var json = $.parseJSON(data);
          if(json["12F"]]) 
              return json["12F"].png;
          else 
              return "";
        }
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM