无法在JavaScript中连接多个JSON

Sorry if my english bad and write comment if you don't understand(i will edit post).I have script that gets json and parse it with order(How I can parse json with arrays by custom order?). It work well for 1 json. My problem is that I need parse several json(like in script 3 json) same time to get correct order of md5's. I tried connect json files to get one json for parsing, but it not connecting. Script:

var jsonForparse =[]
  for (page=1;page<3;page++) {

url = "https://some.url/to/json"+page+".json";
xhr.open('GET', url, false);
xhr.send();
json=xhr.responseText ;
//Parse json string
json=JSON.parse(json);
//Connect jsons to jsonForparse
     js =json.concat(js)

  }
//Parse jsonForparse
md5= ids.map(id => jsonForparse.posts.find(post => post.id === id))

我应该如何解析json或连接json来解析它们以获得md5的正确顺序 例: 应得:

md5=[12,34,56,78,90,100]

订购:

ids=[2227726,2277,2218681,22881,6659,2236659]

Json1:


{
   "posts":[
      {
         "id":2236659,

         "file":{

            "size":1325351,
            "md5":"100"
         }
      },
      {
         "id":2227726,

         "file":{

            "size":1182791,
            "md5":"12"
         }
      },
      {
         "id":2218681,

         "file":{
"size":1241188,
"md5":"56"
         }
      }
   ]
}

Json2:

{
   "posts":[
      {
         "id":6659,

         "file":{

            "size":1325351,
            "md5":"90"
         }
      },
      {
         "id":2277,

         "file":{

            "size":1182791,
            "md5":"34"
         }
      },
      {
         "id":22881,

         "file":{
"size":1241188,
"md5":"78"
         }
      }
   ]
}
评论
miste
miste

您需要像现在一样将每个JSON解析为对象,然后再加入它们:

const obj1 = JSON.parse(json1)
const obj2 = JSON.parse(json2)
... \\ this could be a loop if you have more
const obj = [...obj1.posts, ...obj2.posts].sort((a,b) => a.file.md5 - b.file.md5)
const ids = obj.map(e => e.id)
const md5s = obj.map(e => e.file.md5)
点赞
评论