1

I know there's a json_encode on php. currently we are using that. But it's the only thing that slows the app down cause it needs to go to the server.

Here's the situation, we 2 apps. Let's call them app1 and app2.

App1 is a big application. One of it's input is a php array.

Now app2 on the other hand is a small application created to help app1. App2 has a form. The form has many many fields. Then all of these fields will be converted to the correct php array format, ready to be used as an input in app1.

Now our problem, every time we use app2, we should be more careful or always keep the fields. Cause if there's one field with wrong info, we need to do it again, use app2 to get the correct array.

So, we think we need to add a feature on app2. We want it to be able to edit the php array it produce. We've done that. When php array is given, we send it to server, use json_encode, then put everything in place. Now we have all the fields populated and we can then edit the fields with incorrect data.

At the moment, we are okay with that, but I think it would be more efficient to not go to the server and just convert the php array using js... I've searched google and stackoverflow, I found nothing. I did try using .replace(), but I haven't figured it out yet a good result..

I'm hoping someone can help me here..

EDIT:

it's a PHP array format by the way...

as simple example we have to input fields. After filling out the fields it will output a PHP array format in textarea.

r2k1g
  • 15
  • 4
  • Post the code what have you been trying... – Muthu Kumaran Dec 02 '12 at 06:05
  • Possible duplicate of http://stackoverflow.com/q/5618925/1256403 – Avin Varghese Dec 02 '12 at 06:06
  • That one is using PHP... If ever I needed php, I can always use json_encode.. But I want it to do it on the client side... I've searched for something equivalent to json_encode using javascript, and found none... – r2k1g Dec 02 '12 at 06:15
  • How are you passing a JS object to PHP object without "going to PHP"? These are two languages! Also, relying user input verification on the client side is unsafe. – Passerby Dec 02 '12 at 06:33
  • 1
    You can't have a "PHP array" in JavaScript, so are you saying you have a JSON string that needs parsing, or...? Please show the JS code that you already have and explain clearly where the problem is. – nnnnnn Dec 02 '12 at 06:33
  • We have an application that has a textarea for the output... then that output is a "PHP array format", not a php array object... sorry for the confusion... so, basically it's just a string... I'm trying to parse that string again.... for example I have array('a'=>'goat', 'b' => 'dog') this is a result string of the app, now we want to put it back cause we want to change the dog. Just a simple example though.. – r2k1g Dec 02 '12 at 06:42
  • You want to parse a PHP array literal? In other words, you want to parse PHP... why would you ever output such a thing anyway? You're using PHP to write PHP? At a minimum, I would expect to see a serialized array or something. – Brad Dec 02 '12 at 06:54
  • Instead of figuring out how to parse a PHP array in JS, it sounds like a much, much better option would be rewriting the parts of the applications that deal with data input to use a more workable format. – Corbin Dec 02 '12 at 07:07
  • yes, @Corbin we are not doing that :) – r2k1g Dec 02 '12 at 07:29

2 Answers2

1

From your comment, you seem to be saying you have a string in a textarea like this:

<textarea>array('a'=>'goat', 'b' => 'dog')</textarea>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

I really don't understand why your app would be putting any "code" type result in a textarea in the first place, let alone a string that is a PHP format array literal, but to attempt to answer your question about how to use JavaScript to turn such a string into a JS object, you can do this:

// assuming your textarea has the id "myTextArea":
var str = document.getElementById("myTextArea").value,
    json = "{" + str.slice(6, -1).replace(/=>/g,":").replace(/'/g,"\"") + "}";
console.log(json);
var obj = JSON.parse(json);
console.log(obj);

Demo: http://jsfiddle.net/WknYX/

I put the two console.log() statements in so that you can see what the string looks like after it's been manipulated into JSON format, and then see the resulting object once that JSON has been parsed.

Note that obviously this code will break with an error if the string is in any other format, and if the items in the array contain the characters that are being replaced that would not give an error but would give incorrect results.

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • I can't vote up, but this is almost same as mine, I've used many .replace(), and yes it will break some codes... I think we'll just have to stick with json_encode. I will accept your answer later. – r2k1g Dec 02 '12 at 07:31
0

you can convert a json format under

$array = array("hola","hello");
exit(json_encode($array));



output json // {"hola","hello"}

in js

sucess(x){

}
r2k1g
  • 15
  • 4
Walter Caraza
  • 911
  • 1
  • 10
  • 19