I am wondering how do I make a switch statement in json?
{"Errors":{"key1":"afkafk"},"IsValid":false,"SuccessMessage":""}
I tried
switch(response)
{
case response.Errors.key1:
alert('test');
default:
}
But it seems to ignore my first case.
Edit
// if undefined then go to next if statement - I am not sure if I can do something
// like !=== null
if (response.Errors.key1)
{
// display value of key1
}
else if(response.Errors.Key2)
{
// display value of key2 differently
}
So that is what I am trying to do just with a switch statement.
I am wondering how do I make a switch statement in json?
{"Errors":{"key1":"afkafk"},"IsValid":false,"SuccessMessage":""}
I tried
switch(response)
{
case response.Errors.key1:
alert('test');
default:
}
But it seems to ignore my first case.
Edit
// if undefined then go to next if statement - I am not sure if I can do something
// like !=== null
if (response.Errors.key1)
{
// display value of key1
}
else if(response.Errors.Key2)
{
// display value of key2 differently
}
So that is what I am trying to do just with a switch statement.
Share Improve this question edited Aug 18, 2014 at 4:23 Robby Cornelissen 97.2k23 gold badges149 silver badges174 bronze badges asked Mar 2, 2011 at 22:16 chobo2chobo2 85.8k207 gold badges551 silver badges861 bronze badges 5-
3
That doesn't make any sense.
response
will never equalresponse.Errors.key1
. – Lightness Races in Orbit Commented Mar 2, 2011 at 22:17 -
1
You seem to misunderstand
switch
. What do you think should it do? – user395760 Commented Mar 2, 2011 at 22:17 - Why won't it. response holds the json. I can do response.Errors.Key1 and it would print out the value. So I just need it too look at the key. – chobo2 Commented Mar 2, 2011 at 22:18
- @delnan - I consider Errors to be a dictionary. I want to look at the keys and then do something based on the key it found. – chobo2 Commented Mar 2, 2011 at 22:19
- Correct me if I'm wrong, but this is JavaScript code, not JSON (json/json-en.html) – Gary Commented Dec 8, 2023 at 13:26
3 Answers
Reset to default 10This would be the correct syntax:
switch(response.Errors.key1)
{
case 'afkafk':
alert('test');
break;
default:
alert('default');
}
But I suspect that in your case the following structure would be more adapted:
{ Errors: { key: 'key1', message: 'afkafk' }, IsValid: false, SuccessMessage: '' }
because it would allow you to switch on the key:
switch(response.Errors.key)
{
case 'key1':
alert(response.Errors.message);
break;
default:
alert('default');
}
It sounds like you want to switch on the value key1
instead of the name key1
.
switch (response.Errors.key1) {
case 'afkafk':
...
}
I'm not entirely sure what you're trying to achieve. Are you trying to switch based on the value of key1? Switch statements need to be able to match the variable you pass to the switch statement with the value of a case statement, so the following would work, although I'm not sure if it's what you're after:
switch (response.Errors.key1) {
case 'afkafk': //do something
break;
}