简体   繁体   English

将带有json对象的.js文件中的数据导入到mysql数据库中

[英]Import data from .js file with json object to mysql database

I have a javascript file which includes the data in the following format. 我有一个JavaScript文件,其中包含以下格式的数据。

    index = {
  "me" : {
    "id" : "524bd089-2a7b-4a71-ba23-16354d6351ae",
    "firstName" : "A=",
    "lastName" : "T",
    "pictureName" : "66s2c.jpg",
    "username" : "a-t"
  },
  "spaces" : [ {
    "org" : {
      "id" : "524bd089-2a7b-4a71-ba23-16354d6351ae",
      "firstName" : "An",
      "lastName" : "Tuli",
      "pictureName" : "66s2c.jpg",
      "username" : "arli"
    }
  }, {
    "user" : {
      "id" : "60c4a171-172f-4f66-9014-8b4cf3e476e6",
      "firstName" : "Ban",
      "lastName" : "Idris",
      "pictureName" : "../../../../default-pic/butterfly_200.png",
      "username" : "banun-idris"
    }
  } ]
}
users["524bd089-2a7b-4a71-ba23-16354d6351ae"] = {
  "id" : "524bd089-2a7b-4a71-ba23-16354d6351ae",
  "firstName" : "A",
  "lastName" : "T",
  "pictureName" : "66s2c.jpg",
  "username" : "a",
  "libraries" : [ "lEy27AZavfSR", "l0yApAoo2l4b", "lJl22YOtacxY", "l0UhMCvrMmka", "lJMWpIoFnaK4", "lCZ9cYYjVJcv", "l8kynpyoaej7" ]
}
 libraries["lEy27AZavfSR"] = {
  "id" : "lEy27AZavfSR",
  "name" : "My Main Library",
  "description" : null,
  "numKeeps" : 0,
  "keeps" : [ ]
}

keeps["k4r5UIugqgfk"] = {
  "id" : "k4r5UIugqgfk",
  "keptAt" : 1449613295000,
  "lastActivityAt" : 1449613295000,
  "title" : "",
  "url" : "",
  "note" : null,
  "tags" : [ ],
  "libraries" : [ "lJl22YOtacxY" ],
  "summary" : "",
  "messages" : [ ]
}

I need to import data from this type of format into my mysql database. 我需要将数据从这种类型的格式导入到我的mysql数据库中。

I can't finalize and nail down the approach to do this 我无法最终确定并确定执行此操作的方法

I tried to get the file content into a php file and then convert them into a array string but i get a null result. 我试图将文件内容转换为php文件,然后将其转换为数组字符串,但结果为空。

$jsondata = file_get_contents('trial_data.js');
$data = json_decode($jsondata, true);
var_dump($data);

The second approach I think i can take is load the data in jquery file and send it using AJAX to a php file which adds it to the database. 我认为我可以采用的第二种方法是将数据加载到jquery文件中,然后使用AJAX将其发送到php文件中,然后将其添加到数据库中。

Which approach do you think i should take and how to go about it. 您认为我应该采用哪种方法以及如何进行。

Plus i need to figure out the ID from the first index array, so that I can get the ID from the me and org object for which i need to get the rest of the information 另外,我需要从第一个索引数组中找出ID,以便可以从需要获取其余信息的me和org对象中获取ID。

Since your data is not a valid JSON, you can use a regex to parse it, like this: 由于您的数据不是有效的JSON,因此可以使用正则表达式对其进行解析,如下所示:

$file_content = file_get_contents('trial_data.js');
preg_match_all("/(\w+)\[\"([^\]]*)\"\]\s*\=\s*\{([^\}]*)\}/", $file_content, $matches);
foreach ($matches[1] as $key => $value) {
    $data[$value][$matches[2][$key]] = json_decode('{'.$matches[3][$key].'}');
}
print_r($data);
//For example:
echo $data['users']['524bd089-2a7b-4a71-ba23-16354d6351ae']->pictureName;
//Output: 66s2c.jpg

Demo: https://3v4l.org/fW5gH 演示: https //3v4l.org/fW5gH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM