ovidigital / js-object-to-json Goto Github PK
View Code? Open in Web Editor NEWPHP library to convert a JavaScript object string to JSON format
License: MIT License
PHP library to convert a JavaScript object string to JSON format
License: MIT License
Hi! Thank you for you project!
I hope it can help me to parse data from a JS file.
I have a following example of JS data:
var some_data = {data: s("London")};
where s
is a translation function used in JS code. I need to get an array or PHP object from that.
Unfortunately, such code can't be processed with the json_decode
function that you use under the hood. It says Syntax Error
.
If I call convertToJson
I get:
{"data":"s(""London")}
Double quote appears, which should not be there. Do you think if there's any workaround for that?
Hi Ovi,
Your example code has an error in the capitalization of the namespace:
// Convert the Javascript object to JSON format
$json = \Ovidigital\JsObjectToJson\JsConverter::convertToJson($jsObjectString);
Should actually be:
// Convert the Javascript object to JSON format
$json = \OviDigital\JsObjectToJson\JsConverter::convertToJson($jsObjectString);
This drove me crazy for about half an hour!
Lets say this is your JS object
constmyObject = {
"myPropertyA": "myValue",
"myPropertyB": "myValue"
};
The JS object minified
const myObject={"myPropertyA":"myValue","myPropertyB":"myValue"};
When I use this package, it does not remove the "constmyObject = " or the ";" linebreak at the end of the object.
Please allow for the automatic removal of the object name and the line break at the end of the object.
Example input string for convertToJson:
{"data": true}
Expected result:
{"data": true}
Actual result:
{"data": "true"}
Hi
I found a new case when parsing the javascript object fails:
{
"metadata": "{\"title\":\"Some // Title\"}"
}
The parsed result is NULL
.
{
key: `
some test value1
some test value1
`
}
$convertedString is immediately overridden, please check this code fragment.
Inspection info: Identifies assignments immediately overriding values in non-trivial contexts.
js-object-to-json/src/JsConverter.php
Line 25 in f85b0fb
Hi
The changes introduced in the 1.1.2
version breaks the old parsing mechanism.
Here you can find the example code:
<?php
use OviDigital\JsObjectToJson\JsConverter;
include 'vendor/autoload.php';
$input = '{sources:[],image:"http://127.0.0.1/sapsdvcx3kgq.jpg",duration:"3200",width:"100%"}';
var_dump(JsConverter::convertToJson($input));
Output with version 1.1.1:
string(91) "{"sources":[],"image":"http://127.0.0.1/sapsdvcx3kgq.jpg","duration":"3200","width":"100%"}"
Output with version 1.1.2:
string(30) "{"sources":[],"image:""http":""
^
completly broken
I think the \OviDigital\JsObjectToJson\JsConverter::removeComments()
is not production ready.
Hi!
Found one more case that does not work.
Data for convertToJson
, it's a one-line string:
{id:304,name: "Anna's house",jobs:[{id:3041,title: "Drive to Anna's house.",label: "Drive",steps:3}]}
Expected result:
{"id":304,"name":"Anna's house","jobs":[{"id":3041,"title": "Drive to Anna's house.","label":"Drive","steps":3}]}
Actual result:
{"id":304,"name":"Anna's house\",jobs:[{id:3041,title: \"Drive to Anna's house.","label":"Drive","steps":3}]}
Sample code:
$result = \OviDigital\JsObjectToJson\JsConverter::convertToJson('{id:304,name: "Anna\'s house",jobs:[{id:3041,title: "Drive to Anna\'s house.",label: "Drive",steps:3}]}');
echo $result;
{
key : 'test' // some test value
}
Could you please improve the package to handle this case?
<?php
include 'vendor/autoload.php';
// A variable containing a JavaScript object as a string
$jsObjectString = "{ foo: 'bar', }";
// Convert the Javascript object to JSON format
$json = \OviDigital\JsObjectToJson\JsConverter::convertToJson($jsObjectString);
var_dump($json);
The current output is string(14) "{"foo":"bar",}"
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.