You need to sign in to do that
Don't have an account?
Bhushan.Adhikari
Issue in JSON parsing
I am looking to parse the below json response using Json deserializing
{
"_total": 9,
"values": [
{
"_key": "1050817",
"group": {"id": "1050817"}
}
]
}
The issue i am facing that I cannot declare a variable of name _total , it gives me an error saying no viable alternative at character '_' . Any idea how I can parse the above Json to get the value of total.
{
"_total": 9,
"values": [
{
"_key": "1050817",
"group": {"id": "1050817"}
}
]
}
The issue i am facing that I cannot declare a variable of name _total , it gives me an error saying no viable alternative at character '_' . Any idea how I can parse the above Json to get the value of total.
one option is to replace/remove the _ in the JSON string before deserialising it..
something like
string strJSON = JSONString.replace('_','');
and then use strJSON to deserialize to your object.. so you don't need to declare variables with _ ... you can just call it "total" instead of "_total"
String bodyString = '{ "_total": 9, "values": [ { "_key": "1050817", "group": {"id": "1050817"} } ]}
String Total;
JSONParser parser = JSON.createParser(bodyString);
While (parser.nextToken() != null) {
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == '_total')) {
parser.nextToken();
if( parser.getText() != '' )
total = parser.getText();
}
}
let us know if you got this working.. if our answers helped you to resolve pls mark a best answer..
if you got it working with your own solution please share it with the community for everyones benefit.. :-)
string jsonstr = '{"_total": 9,"values":[{"_key": "1050817","group": {"id": "1050817"}}]}';
Map<String, Object> mastermap = (Map<String, Object>) JSON.deserializeUntyped(jsonstr);
system.debug(mastermap.get('_total'));