Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I got a script which uses an array that was created in PHP and was encoded to JSON. It then take the string and use it to set a value for an element I created:

var val ="";
if(arr != null)
{
 if((arr[i] != undefined) && (arr[i]["value"] != undefined))
 {
     var val = JSON.stringify(arr[i]["value"]);
     val.replace(/"/g , "");
 } 
}
cell.innerHTML = "<input type='text' value='"+val+"' style='padding-right: 0px;'/>";

The problem is that every string on the screen appears with the "" around the string itself. for example "5". I want it to be just 5 and not "5". I thought the replace I did to the val was supposed to do it but it didn't work. why is that?

share|improve this question
1  
show your json value please? –  anant kumar singh 17 hours ago
1  
don't use JSON.stringify , just use arr[i]["value"] –  charlietfl 17 hours ago
    
@charlietfl if I don't use stringify then what is printed are the words Object.object or something like that –  Yonatan Nir 17 hours ago
    
Please show more detail then. If value is number or string it won't print Object. Perhaps you need to check typeof first A demo would help –  charlietfl 17 hours ago
1  
Create a demo using some sample data in jsfiddle.net –  charlietfl 17 hours ago

1 Answer 1

OK the problem is just something I keep forgetting all the time.

The line val.replace(/"/g , ""); is missing its magic and should be:

val = val.replace(/"/g , "");

I'm leaving the question so other people might be able to see how to decode a JSON string and how to use an array which was created in PHP.

share|improve this answer
    
That sounds like a workaround, rather than having addressed the actual issue. Quotes don’t magically appear out of nowhere. –  CBroe 15 hours ago
    
I believe they appear because of the JSON encoding was it not? –  Yonatan Nir 14 hours ago
    
No, the quotes in JSON are part of the syntax, not the data. If after decoding JSON you have quotes that you do not want, then you must have put them in there somewhere in the first place. –  CBroe 14 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.