2016-07-31 46 views
0

任何人都可以帮我解决以下问题。我有2个JSON文件:多个JSON文件调用

  1. names.json:它具有所有的名字年龄和地址阵列(柜面多张居住的)每个人的。 { "people": [ { "name":"Peter Gabriel", "age":42, "address": ["location1","location2","location3"] }, { "name":"Mark Vincent", "age":"25", "address": ["location4","location5"] } ] }

  2. data.json:它具有所有的地址细节 { "location1": { "country":"Switzerland", "street":"some Avenue", "number": 32 }, "location2": { "country":"Latvia", "street":"some Street", "number": 43 }, "location3": { "country":"Denmark", "street":"some Road", "number": 16 }, "location4": { "country":"Austria", "street":"some Avenue", "number": 54 }, "location5": { "country":"Poland", "street":"some Avenue", "number": 23 } } 我需要的data.json文件数据将在全球和names.json之前加载,但作为JSON负载是一个异步函数怎么办我这样做。

    var jsonAddressData = []; 
    function main() 
    { 
        loadNamesJSON(function(response) { 
         jsonAddressData = JSON.parse(response); 
        }); 
    
    
        loadNamesJSON(function(response) { 
         var jsonPeopleData = JSON.parse(response); 
         var addressDetail=[]; 
    
         for(var i in jsonPeopleData.people){ 
          // ACCESS ADDRESS OBJECT DETAILS HERE 
          for(var j in jsonPeopleData.people[i].address){ 
           if (jsonPeopleData.people[i].address[j] in jsonAddressData){ 
    
            addressDetail.append(jsonAddressData[jsonPeopleData.people[i].address[j]]) 
           } 
          } 
         } 
        }); 
    } 
    
    function loadNamesJSON(callback) { 
        var request = new XMLHttpRequest(); 
        request.overrideMimeType("application/json"); 
        request.open('GET', 'names.json', true); 
    
        request.onreadystatechange = function() { 
         if (request.readyState === 4 && request.status ===200) { 
          callback(request.responseText); 
         } 
        }; 
        request.send(null); 
    } 
    
    function loadDataJSON(callback) { 
        var request = new XMLHttpRequest(); 
        request.overrideMimeType("application/json"); 
        request.open('GET', 'data.json', true); 
    
        request.onreadystatechange = function() { 
         if (request.readyState === 4 && request.status ===200) { 
          callback(request.responseText); 
         } 
        }; 
        request.send(null); 
    } 
    
+0

我可以给你适当的例子像这样吗? – Noman

+0

抱歉没有让你 – Deb

+0

检查我的答案。 – Noman

回答

1

在原始的JavaScript,你可以这样做呢:

function phpEncode(obj){ 
    var r = []; 
    if(obj instanceof Array){ 
    for(var i=0,l=obj.length; i<l; i++){ 
     r.push(phpEncode(obj[i])); 
    } 
    return '%5B'+r.join(',')+'%5D'; 
    } 
    else if(typeof obj === 'object' && obj){ 
    for(var i in obj){ 
     if(obj.hasOwnProperty(i)){ 
     var v = obj[i], s; 
     if(typeof v === 'object' && v){ 
      s = encodeURIComponent('"'+i.replace('"', '\\"')+'":')+phpEncode(v); 
     } 
     else{ 
      v = typeof v === 'string' ? '"'+v.replace('"', '\"')+'"' : v; 
      s = encodeURIComponent('"'+i.replace('"', '\\"')+'":'+v); 
     } 
     r.push(s); 
     } 
    } 
    return '%7B'+r.join(',')+'%7D'; 
    } 
    else{ 
    r = typeof obj === 'string' ? '"'+obj.replace('"', '\\"')+'"' : obj; 
    return ''+r; 
    } 
} 
function phpDecode(url){ 
    return eval('('+decodeURIComponent(url)+')'); 
} 
function post(send, where, success, context){ 
    var x = new XMLHttpRequest || new ActiveXObject('Microsoft.XMLHTTP'); 
    var c = context || this; 
    x.open('POST', where); x.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); 
    x.onreadystatechange = function(){ 
    if(x.readyState === 4 && x.status === 200){ 
     if(success)success.call(c, phpDecode(x.responseText)); 
    } 
    } 
    if(typeof send === 'object' && send && !(send instanceof Array)){ 
    var r = []; 
    for(var p in send){ 
     r.push(encodeURIComponent(p)+'='+phpEncode(send[p])); 
    } 
    x.send(r.join('&')); 
    } 
    else{ 
    throw new Error('send must be an Object'); 
    } 
} 
post({}, 'data.json', function(obj1){ 
    // obj1 holds data.json Object 
    post({}, 'names.json', function(obj2){ 
    // obj2 holds names.json Object 
    }); 
}); 

随意修改上面的代码,以满足您的需求。例如,我正在使用POST请求。当然,你的情况并不重要。使用jQuery

如果:

$.getJSON('data.json', function(obj1){ 
    // obj1 holds data.json Object 
    $.getJSON('names.json', function(obj2){ 
    // obj2 holds names.json Object 
    }); 
}); 

重要的是要注意的是,AJAX是异步的,所以后面一定是成功的函数内的所有代码。换句话说,如果你这样做:

$.getJSON('data.json', function(obj1){ 
    var whatever = obj1; 
    // obj1 holds data.json Object 
    $.getJSON('names.json', function(obj2){ 
    // obj2 holds names.json Object 
    }); 
}); 
console.log(whatever); 

console.log(whatever)将返回undefined,因为除非它发生了超高速没有发生AJAX。无论如何,它需要一段时间才能完全理解这个东西。祝你好运。

+0

嗨PHPglue,测试了jQuery版本,它的工作,谢谢。 :) – Deb