-1

I am getting server response json with special character, How can i remove this special charecter from it. It tried replace() but its not working. My json sample is here

{
     "@tag":"1013170",
     "@title":"Holman Rd & Trestle Glen Rd",
     "@lat":"37.8067794",
     "@lon":"-122.2325773",
     "@stopId":"52750"
 }
Muhammad Aamir Ali
  • 20,419
  • 10
  • 66
  • 57

3 Answers3

2

try something like this

    var str =   '{"@tag":"1013170","@title":"Holman Rd & Trestle Glen Rd","@lat":"37.8067794","@lon":"-122.2325773","@stopId":"52750"}';
    console.log(str.replace(/@/g,''));

    //will give you
    {"tag":"1013170","title":"Holman Rd & Trestle Glen Rd","lat":"37.8067794","lon":"-122.2325773","stopId":"52750"}

By the way your json is valid

var json ={"@tag":"1013170","@title":"Holman Rd & Trestle Glen Rd"};

//var json_obj = $.parseJSON(json);//don't do this because it already json.

console.log(json.@tag); // don't use this way

console.log(json['@tag']);// instead try this way
rajesh kakawat
  • 10,826
  • 1
  • 21
  • 40
2

You can't change a property name, you have to add the value with a new name and delete the old property:

object = {
     "@tag":"1013170",
     "@title":"Holman Rd & Trestle Glen Rd",
     "@lat":"37.8067794",
     "@lon":"-122.2325773",
     "@stopId":"52750"
 }
for (var property in object) {
    if (object.hasOwnProperty(property)) {
        object[property.slice(1)]=object[property];
        delete object[property];
    }
}
console.log(object) will now give you
{tag: "1013170", title: "Holman Rd & Trestle Glen Rd", lat: "37.8067794", lon: "-122.2325773", stopId: "52750"}
kaushikb9
  • 231
  • 2
  • 6
  • 1
    I guess the question is: What's the point of doing this in the first place? What the OP posted is syntactically correct, so why do you think that replacing `@` fixes anything? – Felix Kling Dec 26 '13 at 08:07
  • @Kaushik Bhat Thank you so much. You saved my time worked like a charme +1 – Muhammad Aamir Ali Dec 26 '13 at 08:09
  • 2
    @Muhammad: I'm curious, what exactly was the problem? – Felix Kling Dec 26 '13 at 08:10
  • @FelixKling i had to remove the first place i.e '@', Kaushik answer doing exactly the same. – Muhammad Aamir Ali Dec 26 '13 at 08:13
  • @Muhammad: But why did you have to remove it? What exactly are you doing with the data that would cause `@` to be a problem? Because property names can contain *any* character. – Felix Kling Dec 26 '13 at 08:14
  • @FelixKling it was causing syntax error (unexpected token illegal) when i read it. – Muhammad Aamir Ali Dec 26 '13 at 08:16
  • 1
    @Muhammad: Again: The `@` is valid there, so parsing the JSON should work without problems. Kaushik's solution doesn't even consider the parsing step, it builds on top of an already existing object. So, what exactly do you mean by "read it" and what *exactly* are you doing with the data? How are you trying to process it? Please update your question and add the corresponding code. As it stands now, the question is incomplete since it's not possible to reproduce the problem with the given information, and it's far from obvious how this answer actually fixes the "problem". – Felix Kling Dec 26 '13 at 08:20
  • @FelixKling sorry Felix Kaushik answer did not work to me, initially it seems that it working but it was not. I post my answer that how i fixed it Thanks. – Muhammad Aamir Ali Dec 26 '13 at 10:25
0

Finally i read the json with this line.

result['@tag']

Its simple you can read json from '.' notation and from [], the second one reads special character as well.

Muhammad Aamir Ali
  • 20,419
  • 10
  • 66
  • 57
  • 1
    See, if you had properly described the problem, and explained that you get an error when you try to do `result.@tag`, then I could have forwarded you to http://stackoverflow.com/q/12953704/218196. Anyways, glad you figured it out. rajesh actually [already provided this as an solution though](http://stackoverflow.com/a/20781257/218196). – Felix Kling Dec 26 '13 at 17:44