I have a javascript file which includes the data in the following format.
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.
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.
$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.
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
Since your data is not a valid JSON, you can use a regex to parse it, like this:
$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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.