JSON Parser getCurrentToken()
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
5
down vote
favorite
I have a JSON string that I want to parse out. I have use the below code many times but for whatever reason this time it is not working. Can anyone poke any holes in this logic? Or point out why this may not be the best way to parse through a string?
String tokencred = ["Id":"003m000000yKLySkkL","Token":"I2j5bUky04B4eLJGYELV"]
JSONParser parser = JSON.createParser(tokencred);
//parse and map the returned string
while (parser.nextToken() != null)
String parsedId;
String parsedToken;
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'Id'))
parser.nextToken();
parsedId = parser.getText();
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'Token'))
parser.nextToken();
parsedToken = parser.getText();
tokenCredMap.put(parsedId,parsedToken);
system.debug('tokenCredMap:: ' + tokenCredMap);
14:31:15.0 (808193208)|USER_DEBUG|[153]|DEBUG|tokenCredMap:: null=null, 003m000000yKLySkkL=null
I do need the map as there is additional logic that will enable me to have multiple token/id combos.
UPDATE:
15:02:55.11 (726597242)|USER_DEBUG|[314]|DEBUG|this is the updated value ["Id":"00Qm0000009qqdiEAA","Token":"J5h8yz004mFJi44ozuhw"]
15:02:55.11 (727270334)|USER_DEBUG|[323]|DEBUG|fieldName: Id
15:02:55.11 (727412370)|USER_DEBUG|[323]|DEBUG|fieldName: Token
15:02:55.11 (727597218)|USER_DEBUG|[342]|DEBUG|tokenCredMap:: null=null, 00Qm0000009qqdiEAA=null
15:02:55.11 (732474914)|DML_BEGIN|[360]|Op:Update|Type:Lead|Rows:1
apex json string parser
add a comment |Â
up vote
5
down vote
favorite
I have a JSON string that I want to parse out. I have use the below code many times but for whatever reason this time it is not working. Can anyone poke any holes in this logic? Or point out why this may not be the best way to parse through a string?
String tokencred = ["Id":"003m000000yKLySkkL","Token":"I2j5bUky04B4eLJGYELV"]
JSONParser parser = JSON.createParser(tokencred);
//parse and map the returned string
while (parser.nextToken() != null)
String parsedId;
String parsedToken;
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'Id'))
parser.nextToken();
parsedId = parser.getText();
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'Token'))
parser.nextToken();
parsedToken = parser.getText();
tokenCredMap.put(parsedId,parsedToken);
system.debug('tokenCredMap:: ' + tokenCredMap);
14:31:15.0 (808193208)|USER_DEBUG|[153]|DEBUG|tokenCredMap:: null=null, 003m000000yKLySkkL=null
I do need the map as there is additional logic that will enable me to have multiple token/id combos.
UPDATE:
15:02:55.11 (726597242)|USER_DEBUG|[314]|DEBUG|this is the updated value ["Id":"00Qm0000009qqdiEAA","Token":"J5h8yz004mFJi44ozuhw"]
15:02:55.11 (727270334)|USER_DEBUG|[323]|DEBUG|fieldName: Id
15:02:55.11 (727412370)|USER_DEBUG|[323]|DEBUG|fieldName: Token
15:02:55.11 (727597218)|USER_DEBUG|[342]|DEBUG|tokenCredMap:: null=null, 00Qm0000009qqdiEAA=null
15:02:55.11 (732474914)|DML_BEGIN|[360]|Op:Update|Type:Lead|Rows:1
apex json string parser
"why this may not be the best way to parse through a string" - because JSON.deserializeUntyped in this case is much easier :)
– Eric
Sep 6 at 2:41
add a comment |Â
up vote
5
down vote
favorite
up vote
5
down vote
favorite
I have a JSON string that I want to parse out. I have use the below code many times but for whatever reason this time it is not working. Can anyone poke any holes in this logic? Or point out why this may not be the best way to parse through a string?
String tokencred = ["Id":"003m000000yKLySkkL","Token":"I2j5bUky04B4eLJGYELV"]
JSONParser parser = JSON.createParser(tokencred);
//parse and map the returned string
while (parser.nextToken() != null)
String parsedId;
String parsedToken;
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'Id'))
parser.nextToken();
parsedId = parser.getText();
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'Token'))
parser.nextToken();
parsedToken = parser.getText();
tokenCredMap.put(parsedId,parsedToken);
system.debug('tokenCredMap:: ' + tokenCredMap);
14:31:15.0 (808193208)|USER_DEBUG|[153]|DEBUG|tokenCredMap:: null=null, 003m000000yKLySkkL=null
I do need the map as there is additional logic that will enable me to have multiple token/id combos.
UPDATE:
15:02:55.11 (726597242)|USER_DEBUG|[314]|DEBUG|this is the updated value ["Id":"00Qm0000009qqdiEAA","Token":"J5h8yz004mFJi44ozuhw"]
15:02:55.11 (727270334)|USER_DEBUG|[323]|DEBUG|fieldName: Id
15:02:55.11 (727412370)|USER_DEBUG|[323]|DEBUG|fieldName: Token
15:02:55.11 (727597218)|USER_DEBUG|[342]|DEBUG|tokenCredMap:: null=null, 00Qm0000009qqdiEAA=null
15:02:55.11 (732474914)|DML_BEGIN|[360]|Op:Update|Type:Lead|Rows:1
apex json string parser
I have a JSON string that I want to parse out. I have use the below code many times but for whatever reason this time it is not working. Can anyone poke any holes in this logic? Or point out why this may not be the best way to parse through a string?
String tokencred = ["Id":"003m000000yKLySkkL","Token":"I2j5bUky04B4eLJGYELV"]
JSONParser parser = JSON.createParser(tokencred);
//parse and map the returned string
while (parser.nextToken() != null)
String parsedId;
String parsedToken;
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'Id'))
parser.nextToken();
parsedId = parser.getText();
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'Token'))
parser.nextToken();
parsedToken = parser.getText();
tokenCredMap.put(parsedId,parsedToken);
system.debug('tokenCredMap:: ' + tokenCredMap);
14:31:15.0 (808193208)|USER_DEBUG|[153]|DEBUG|tokenCredMap:: null=null, 003m000000yKLySkkL=null
I do need the map as there is additional logic that will enable me to have multiple token/id combos.
UPDATE:
15:02:55.11 (726597242)|USER_DEBUG|[314]|DEBUG|this is the updated value ["Id":"00Qm0000009qqdiEAA","Token":"J5h8yz004mFJi44ozuhw"]
15:02:55.11 (727270334)|USER_DEBUG|[323]|DEBUG|fieldName: Id
15:02:55.11 (727412370)|USER_DEBUG|[323]|DEBUG|fieldName: Token
15:02:55.11 (727597218)|USER_DEBUG|[342]|DEBUG|tokenCredMap:: null=null, 00Qm0000009qqdiEAA=null
15:02:55.11 (732474914)|DML_BEGIN|[360]|Op:Update|Type:Lead|Rows:1
apex json string parser
edited Sep 5 at 22:10
asked Sep 5 at 21:46
Olivia
1,135317
1,135317
"why this may not be the best way to parse through a string" - because JSON.deserializeUntyped in this case is much easier :)
– Eric
Sep 6 at 2:41
add a comment |Â
"why this may not be the best way to parse through a string" - because JSON.deserializeUntyped in this case is much easier :)
– Eric
Sep 6 at 2:41
"why this may not be the best way to parse through a string" - because JSON.deserializeUntyped in this case is much easier :)
– Eric
Sep 6 at 2:41
"why this may not be the best way to parse through a string" - because JSON.deserializeUntyped in this case is much easier :)
– Eric
Sep 6 at 2:41
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
5
down vote
I think the issue is that you are not advancing the parser after saving the ParsedId. Let's say that the if statement for the Id is evaluated as true. You then save the ParsedId, but you are not advancing the parser forward afterwords. That means the second If statement can never be true because the parser is still pointing to the value of the Id.
2
This is the most direct answer to the question.
– gNerb
Sep 5 at 22:06
Yep I agree with gNerb about that.
– Keith C
Sep 5 at 22:10
@Hans Shouldn't the while loop allow the parser to loop through all the values thus allowing it to see the token value?
– Olivia
Sep 6 at 14:32
It does get through all the values, but you're re-declaring both values on each loop. I believe this is overwriting whatever you got from the previous loop. for example, if you got the Id on the first loop, you add it to the map with a null value pair (Since we haven't found the token yet). When we loop the next time, both values are re-initialized to null. When we get the token this loop, the Id is now null so a null key is added to the map with the value of the token I believe
– Hans Donkersloot
Sep 6 at 14:36
add a comment |Â
up vote
3
down vote
If would write that like this:
Map<String, String> tokenCredMap = new Map<String, String>();
String tokencred = '["Id":"003m000000yKLySkkL","Token":"I2j5bUky04B4eLJGYELV"]';
List<Object> items = (List<Object>) JSON.deserializeUntyped(tokencred);
for (object item : items)
Map<String, Object> typedItem = (Map<String, Object>) item;
tokenCredMap.put((Id) typedItem.get('Id'), (String) typedItem.get('Token'));
system.debug('tokenCredMap:: ' + tokenCredMap);
as it leaves working at the token level to the deserializeUntyped
method and so avoids the risk of bugs in that area.
(In your code often both parsedId
and parsedToken
will be null and if other attributes were in the JSON your code would be tripped up. Also in JSON the order of the attributes is meant to be open and your logic requires the attributes to be in a specific order.)
This is the best answer (posted so far) although it doesn't explain why what the OP is doing doesn't work. I was in the process of writing an answer that combined both of the answers that were just posted but figured not much point anymore :P)
– gNerb
Sep 5 at 22:07
1
@gNerb Yeah I think Hans is describing the core problem and I'm just commenting how fragile in general a hand-written parser is.
– Keith C
Sep 5 at 22:09
this is great. thank you for posting a different look at parsing. Unfortunately it is still not parsing out the token for what ever reason. What confuses me is: if there was another process that was rolling it back then the ID wouldn't be saved to the map, right? I will update my post with the results from your solution.
– Olivia
Sep 5 at 22:09
@Olivia The best way to develop logic like this is to use a unit test so you can debug a simple case.
– Keith C
Sep 5 at 22:12
Keith I had a hard time getting your sample to work due to some run time type conversion errors. I updated your code with a working sample. Hope you don't mind.
– gNerb
Sep 5 at 22:17
 |Â
show 2 more comments
up vote
0
down vote
accepted
I found the issue: the parsedId
and the parsedToken
need to be on the outside of the loop. This is another way to loop through the parser that I found and it is working:
String parsedId;
String parsedToken;
while (parser.nextToken() != null)
if (parser.getCurrentToken() == JSONToken.FIELD_NAME)
String fieldName = parser.getText();
System.debug('fieldName: ' + fieldName);
//move to token after the field name
parser.nextToken();
if (fieldName == 'Id')
parsedId = parser.getText();
else if (fieldName == 'Token')
parsedToken = parser.getText();
tokenCredMap.put(parsedId,parsedToken);
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
I think the issue is that you are not advancing the parser after saving the ParsedId. Let's say that the if statement for the Id is evaluated as true. You then save the ParsedId, but you are not advancing the parser forward afterwords. That means the second If statement can never be true because the parser is still pointing to the value of the Id.
2
This is the most direct answer to the question.
– gNerb
Sep 5 at 22:06
Yep I agree with gNerb about that.
– Keith C
Sep 5 at 22:10
@Hans Shouldn't the while loop allow the parser to loop through all the values thus allowing it to see the token value?
– Olivia
Sep 6 at 14:32
It does get through all the values, but you're re-declaring both values on each loop. I believe this is overwriting whatever you got from the previous loop. for example, if you got the Id on the first loop, you add it to the map with a null value pair (Since we haven't found the token yet). When we loop the next time, both values are re-initialized to null. When we get the token this loop, the Id is now null so a null key is added to the map with the value of the token I believe
– Hans Donkersloot
Sep 6 at 14:36
add a comment |Â
up vote
5
down vote
I think the issue is that you are not advancing the parser after saving the ParsedId. Let's say that the if statement for the Id is evaluated as true. You then save the ParsedId, but you are not advancing the parser forward afterwords. That means the second If statement can never be true because the parser is still pointing to the value of the Id.
2
This is the most direct answer to the question.
– gNerb
Sep 5 at 22:06
Yep I agree with gNerb about that.
– Keith C
Sep 5 at 22:10
@Hans Shouldn't the while loop allow the parser to loop through all the values thus allowing it to see the token value?
– Olivia
Sep 6 at 14:32
It does get through all the values, but you're re-declaring both values on each loop. I believe this is overwriting whatever you got from the previous loop. for example, if you got the Id on the first loop, you add it to the map with a null value pair (Since we haven't found the token yet). When we loop the next time, both values are re-initialized to null. When we get the token this loop, the Id is now null so a null key is added to the map with the value of the token I believe
– Hans Donkersloot
Sep 6 at 14:36
add a comment |Â
up vote
5
down vote
up vote
5
down vote
I think the issue is that you are not advancing the parser after saving the ParsedId. Let's say that the if statement for the Id is evaluated as true. You then save the ParsedId, but you are not advancing the parser forward afterwords. That means the second If statement can never be true because the parser is still pointing to the value of the Id.
I think the issue is that you are not advancing the parser after saving the ParsedId. Let's say that the if statement for the Id is evaluated as true. You then save the ParsedId, but you are not advancing the parser forward afterwords. That means the second If statement can never be true because the parser is still pointing to the value of the Id.
answered Sep 5 at 22:06


Hans Donkersloot
783
783
2
This is the most direct answer to the question.
– gNerb
Sep 5 at 22:06
Yep I agree with gNerb about that.
– Keith C
Sep 5 at 22:10
@Hans Shouldn't the while loop allow the parser to loop through all the values thus allowing it to see the token value?
– Olivia
Sep 6 at 14:32
It does get through all the values, but you're re-declaring both values on each loop. I believe this is overwriting whatever you got from the previous loop. for example, if you got the Id on the first loop, you add it to the map with a null value pair (Since we haven't found the token yet). When we loop the next time, both values are re-initialized to null. When we get the token this loop, the Id is now null so a null key is added to the map with the value of the token I believe
– Hans Donkersloot
Sep 6 at 14:36
add a comment |Â
2
This is the most direct answer to the question.
– gNerb
Sep 5 at 22:06
Yep I agree with gNerb about that.
– Keith C
Sep 5 at 22:10
@Hans Shouldn't the while loop allow the parser to loop through all the values thus allowing it to see the token value?
– Olivia
Sep 6 at 14:32
It does get through all the values, but you're re-declaring both values on each loop. I believe this is overwriting whatever you got from the previous loop. for example, if you got the Id on the first loop, you add it to the map with a null value pair (Since we haven't found the token yet). When we loop the next time, both values are re-initialized to null. When we get the token this loop, the Id is now null so a null key is added to the map with the value of the token I believe
– Hans Donkersloot
Sep 6 at 14:36
2
2
This is the most direct answer to the question.
– gNerb
Sep 5 at 22:06
This is the most direct answer to the question.
– gNerb
Sep 5 at 22:06
Yep I agree with gNerb about that.
– Keith C
Sep 5 at 22:10
Yep I agree with gNerb about that.
– Keith C
Sep 5 at 22:10
@Hans Shouldn't the while loop allow the parser to loop through all the values thus allowing it to see the token value?
– Olivia
Sep 6 at 14:32
@Hans Shouldn't the while loop allow the parser to loop through all the values thus allowing it to see the token value?
– Olivia
Sep 6 at 14:32
It does get through all the values, but you're re-declaring both values on each loop. I believe this is overwriting whatever you got from the previous loop. for example, if you got the Id on the first loop, you add it to the map with a null value pair (Since we haven't found the token yet). When we loop the next time, both values are re-initialized to null. When we get the token this loop, the Id is now null so a null key is added to the map with the value of the token I believe
– Hans Donkersloot
Sep 6 at 14:36
It does get through all the values, but you're re-declaring both values on each loop. I believe this is overwriting whatever you got from the previous loop. for example, if you got the Id on the first loop, you add it to the map with a null value pair (Since we haven't found the token yet). When we loop the next time, both values are re-initialized to null. When we get the token this loop, the Id is now null so a null key is added to the map with the value of the token I believe
– Hans Donkersloot
Sep 6 at 14:36
add a comment |Â
up vote
3
down vote
If would write that like this:
Map<String, String> tokenCredMap = new Map<String, String>();
String tokencred = '["Id":"003m000000yKLySkkL","Token":"I2j5bUky04B4eLJGYELV"]';
List<Object> items = (List<Object>) JSON.deserializeUntyped(tokencred);
for (object item : items)
Map<String, Object> typedItem = (Map<String, Object>) item;
tokenCredMap.put((Id) typedItem.get('Id'), (String) typedItem.get('Token'));
system.debug('tokenCredMap:: ' + tokenCredMap);
as it leaves working at the token level to the deserializeUntyped
method and so avoids the risk of bugs in that area.
(In your code often both parsedId
and parsedToken
will be null and if other attributes were in the JSON your code would be tripped up. Also in JSON the order of the attributes is meant to be open and your logic requires the attributes to be in a specific order.)
This is the best answer (posted so far) although it doesn't explain why what the OP is doing doesn't work. I was in the process of writing an answer that combined both of the answers that were just posted but figured not much point anymore :P)
– gNerb
Sep 5 at 22:07
1
@gNerb Yeah I think Hans is describing the core problem and I'm just commenting how fragile in general a hand-written parser is.
– Keith C
Sep 5 at 22:09
this is great. thank you for posting a different look at parsing. Unfortunately it is still not parsing out the token for what ever reason. What confuses me is: if there was another process that was rolling it back then the ID wouldn't be saved to the map, right? I will update my post with the results from your solution.
– Olivia
Sep 5 at 22:09
@Olivia The best way to develop logic like this is to use a unit test so you can debug a simple case.
– Keith C
Sep 5 at 22:12
Keith I had a hard time getting your sample to work due to some run time type conversion errors. I updated your code with a working sample. Hope you don't mind.
– gNerb
Sep 5 at 22:17
 |Â
show 2 more comments
up vote
3
down vote
If would write that like this:
Map<String, String> tokenCredMap = new Map<String, String>();
String tokencred = '["Id":"003m000000yKLySkkL","Token":"I2j5bUky04B4eLJGYELV"]';
List<Object> items = (List<Object>) JSON.deserializeUntyped(tokencred);
for (object item : items)
Map<String, Object> typedItem = (Map<String, Object>) item;
tokenCredMap.put((Id) typedItem.get('Id'), (String) typedItem.get('Token'));
system.debug('tokenCredMap:: ' + tokenCredMap);
as it leaves working at the token level to the deserializeUntyped
method and so avoids the risk of bugs in that area.
(In your code often both parsedId
and parsedToken
will be null and if other attributes were in the JSON your code would be tripped up. Also in JSON the order of the attributes is meant to be open and your logic requires the attributes to be in a specific order.)
This is the best answer (posted so far) although it doesn't explain why what the OP is doing doesn't work. I was in the process of writing an answer that combined both of the answers that were just posted but figured not much point anymore :P)
– gNerb
Sep 5 at 22:07
1
@gNerb Yeah I think Hans is describing the core problem and I'm just commenting how fragile in general a hand-written parser is.
– Keith C
Sep 5 at 22:09
this is great. thank you for posting a different look at parsing. Unfortunately it is still not parsing out the token for what ever reason. What confuses me is: if there was another process that was rolling it back then the ID wouldn't be saved to the map, right? I will update my post with the results from your solution.
– Olivia
Sep 5 at 22:09
@Olivia The best way to develop logic like this is to use a unit test so you can debug a simple case.
– Keith C
Sep 5 at 22:12
Keith I had a hard time getting your sample to work due to some run time type conversion errors. I updated your code with a working sample. Hope you don't mind.
– gNerb
Sep 5 at 22:17
 |Â
show 2 more comments
up vote
3
down vote
up vote
3
down vote
If would write that like this:
Map<String, String> tokenCredMap = new Map<String, String>();
String tokencred = '["Id":"003m000000yKLySkkL","Token":"I2j5bUky04B4eLJGYELV"]';
List<Object> items = (List<Object>) JSON.deserializeUntyped(tokencred);
for (object item : items)
Map<String, Object> typedItem = (Map<String, Object>) item;
tokenCredMap.put((Id) typedItem.get('Id'), (String) typedItem.get('Token'));
system.debug('tokenCredMap:: ' + tokenCredMap);
as it leaves working at the token level to the deserializeUntyped
method and so avoids the risk of bugs in that area.
(In your code often both parsedId
and parsedToken
will be null and if other attributes were in the JSON your code would be tripped up. Also in JSON the order of the attributes is meant to be open and your logic requires the attributes to be in a specific order.)
If would write that like this:
Map<String, String> tokenCredMap = new Map<String, String>();
String tokencred = '["Id":"003m000000yKLySkkL","Token":"I2j5bUky04B4eLJGYELV"]';
List<Object> items = (List<Object>) JSON.deserializeUntyped(tokencred);
for (object item : items)
Map<String, Object> typedItem = (Map<String, Object>) item;
tokenCredMap.put((Id) typedItem.get('Id'), (String) typedItem.get('Token'));
system.debug('tokenCredMap:: ' + tokenCredMap);
as it leaves working at the token level to the deserializeUntyped
method and so avoids the risk of bugs in that area.
(In your code often both parsedId
and parsedToken
will be null and if other attributes were in the JSON your code would be tripped up. Also in JSON the order of the attributes is meant to be open and your logic requires the attributes to be in a specific order.)
edited Sep 5 at 22:16
gNerb
4,969634
4,969634
answered Sep 5 at 22:04
Keith C
90.6k1082184
90.6k1082184
This is the best answer (posted so far) although it doesn't explain why what the OP is doing doesn't work. I was in the process of writing an answer that combined both of the answers that were just posted but figured not much point anymore :P)
– gNerb
Sep 5 at 22:07
1
@gNerb Yeah I think Hans is describing the core problem and I'm just commenting how fragile in general a hand-written parser is.
– Keith C
Sep 5 at 22:09
this is great. thank you for posting a different look at parsing. Unfortunately it is still not parsing out the token for what ever reason. What confuses me is: if there was another process that was rolling it back then the ID wouldn't be saved to the map, right? I will update my post with the results from your solution.
– Olivia
Sep 5 at 22:09
@Olivia The best way to develop logic like this is to use a unit test so you can debug a simple case.
– Keith C
Sep 5 at 22:12
Keith I had a hard time getting your sample to work due to some run time type conversion errors. I updated your code with a working sample. Hope you don't mind.
– gNerb
Sep 5 at 22:17
 |Â
show 2 more comments
This is the best answer (posted so far) although it doesn't explain why what the OP is doing doesn't work. I was in the process of writing an answer that combined both of the answers that were just posted but figured not much point anymore :P)
– gNerb
Sep 5 at 22:07
1
@gNerb Yeah I think Hans is describing the core problem and I'm just commenting how fragile in general a hand-written parser is.
– Keith C
Sep 5 at 22:09
this is great. thank you for posting a different look at parsing. Unfortunately it is still not parsing out the token for what ever reason. What confuses me is: if there was another process that was rolling it back then the ID wouldn't be saved to the map, right? I will update my post with the results from your solution.
– Olivia
Sep 5 at 22:09
@Olivia The best way to develop logic like this is to use a unit test so you can debug a simple case.
– Keith C
Sep 5 at 22:12
Keith I had a hard time getting your sample to work due to some run time type conversion errors. I updated your code with a working sample. Hope you don't mind.
– gNerb
Sep 5 at 22:17
This is the best answer (posted so far) although it doesn't explain why what the OP is doing doesn't work. I was in the process of writing an answer that combined both of the answers that were just posted but figured not much point anymore :P)
– gNerb
Sep 5 at 22:07
This is the best answer (posted so far) although it doesn't explain why what the OP is doing doesn't work. I was in the process of writing an answer that combined both of the answers that were just posted but figured not much point anymore :P)
– gNerb
Sep 5 at 22:07
1
1
@gNerb Yeah I think Hans is describing the core problem and I'm just commenting how fragile in general a hand-written parser is.
– Keith C
Sep 5 at 22:09
@gNerb Yeah I think Hans is describing the core problem and I'm just commenting how fragile in general a hand-written parser is.
– Keith C
Sep 5 at 22:09
this is great. thank you for posting a different look at parsing. Unfortunately it is still not parsing out the token for what ever reason. What confuses me is: if there was another process that was rolling it back then the ID wouldn't be saved to the map, right? I will update my post with the results from your solution.
– Olivia
Sep 5 at 22:09
this is great. thank you for posting a different look at parsing. Unfortunately it is still not parsing out the token for what ever reason. What confuses me is: if there was another process that was rolling it back then the ID wouldn't be saved to the map, right? I will update my post with the results from your solution.
– Olivia
Sep 5 at 22:09
@Olivia The best way to develop logic like this is to use a unit test so you can debug a simple case.
– Keith C
Sep 5 at 22:12
@Olivia The best way to develop logic like this is to use a unit test so you can debug a simple case.
– Keith C
Sep 5 at 22:12
Keith I had a hard time getting your sample to work due to some run time type conversion errors. I updated your code with a working sample. Hope you don't mind.
– gNerb
Sep 5 at 22:17
Keith I had a hard time getting your sample to work due to some run time type conversion errors. I updated your code with a working sample. Hope you don't mind.
– gNerb
Sep 5 at 22:17
 |Â
show 2 more comments
up vote
0
down vote
accepted
I found the issue: the parsedId
and the parsedToken
need to be on the outside of the loop. This is another way to loop through the parser that I found and it is working:
String parsedId;
String parsedToken;
while (parser.nextToken() != null)
if (parser.getCurrentToken() == JSONToken.FIELD_NAME)
String fieldName = parser.getText();
System.debug('fieldName: ' + fieldName);
//move to token after the field name
parser.nextToken();
if (fieldName == 'Id')
parsedId = parser.getText();
else if (fieldName == 'Token')
parsedToken = parser.getText();
tokenCredMap.put(parsedId,parsedToken);
add a comment |Â
up vote
0
down vote
accepted
I found the issue: the parsedId
and the parsedToken
need to be on the outside of the loop. This is another way to loop through the parser that I found and it is working:
String parsedId;
String parsedToken;
while (parser.nextToken() != null)
if (parser.getCurrentToken() == JSONToken.FIELD_NAME)
String fieldName = parser.getText();
System.debug('fieldName: ' + fieldName);
//move to token after the field name
parser.nextToken();
if (fieldName == 'Id')
parsedId = parser.getText();
else if (fieldName == 'Token')
parsedToken = parser.getText();
tokenCredMap.put(parsedId,parsedToken);
add a comment |Â
up vote
0
down vote
accepted
up vote
0
down vote
accepted
I found the issue: the parsedId
and the parsedToken
need to be on the outside of the loop. This is another way to loop through the parser that I found and it is working:
String parsedId;
String parsedToken;
while (parser.nextToken() != null)
if (parser.getCurrentToken() == JSONToken.FIELD_NAME)
String fieldName = parser.getText();
System.debug('fieldName: ' + fieldName);
//move to token after the field name
parser.nextToken();
if (fieldName == 'Id')
parsedId = parser.getText();
else if (fieldName == 'Token')
parsedToken = parser.getText();
tokenCredMap.put(parsedId,parsedToken);
I found the issue: the parsedId
and the parsedToken
need to be on the outside of the loop. This is another way to loop through the parser that I found and it is working:
String parsedId;
String parsedToken;
while (parser.nextToken() != null)
if (parser.getCurrentToken() == JSONToken.FIELD_NAME)
String fieldName = parser.getText();
System.debug('fieldName: ' + fieldName);
//move to token after the field name
parser.nextToken();
if (fieldName == 'Id')
parsedId = parser.getText();
else if (fieldName == 'Token')
parsedToken = parser.getText();
tokenCredMap.put(parsedId,parsedToken);
edited Sep 6 at 15:14
answered Sep 6 at 14:35
Olivia
1,135317
1,135317
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f231391%2fjson-parser-getcurrenttoken%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
"why this may not be the best way to parse through a string" - because JSON.deserializeUntyped in this case is much easier :)
– Eric
Sep 6 at 2:41