0

I have a JSON response returning special characters in the response below.

[{"@networkName":"nameOfNetwork1"}, {"@networkName":"nameOfNetwork2"}]

Does anyone know how to get rid of the '@' symbol or a way around it? Or how I could get retrieve it so I can display the network name in my view?

1
  • 2
    That's perfectly valid JSON so it wouldn't get rid of the @. You could access it as is (myObject["@networkName"]) or replace it at some point (myObject.networkName = myObject["@networkName"]) Commented Feb 9, 2016 at 23:36

1 Answer 1

2

There are various options you have how you can access the your JSON to display the values, which are to use bracket [] notation or add a new property to your JSON so you can use dot . notation.

Here are some simple ways of doing both. (To see the results of what is being output open your browser developer tools > console)

The first console.log() accesses the values using bracket notation.

The for loop adds a new key value pair into your JSON.

The second console.log() access the values using dot . notation to access the newly created value

var obj = [
  {"@networkName":"nameOfNetwork1"}, 
  {"@networkName":"nameOfNetwork2"}
];

console.log(obj[0]["@networkName"]);
// This will log "nameOfNetwork1"

for(var i = 0; i < obj.length; i++) {
  // To will add a new key value into your data with a key name of newName
  obj[i].networkName = obj[i]["@networkName"];
}

console.log(obj[0].networkName);
// This will log "nameOfNetwork1"

Sign up to request clarification or add additional context in comments.

1 Comment

No problem, glad it could help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.