You need to sign in to do that
Don't have an account?
Manish Gupta 122
JSONException: Unexpected character ('I' (code 73)): was expecting double-quote to start field name
Hi Developers,
I am facing issue when i am passing a json string and it is not getting deserialized as it is considering the whole json string as one value.
Here is my code and json string:
Code:
public override String abc(){
Map<String, Object> xyz = (Map<String, Object> )JSON.deserializeUntyped(jsonString);
Account acc = new Account();
system.debug('@@@@@'+xyz );
for (String key : xyz.keySet()){
String objString = (String)xyz.get(key);
if( key == 'Account'){
acc = (Account)JSON.deserialize(objString, Account.class);
}
}
jsonString:
'{"Account":"{ "Id ": "001550 ","ShippingStreet ": "abc ", "ShippingState ": "ON ", "ShippingCountry ": "A ", "ShippingCity ": "G ", "Phone ": "123456789 ", "BillingCountry ": "ABC ", "BillingCity ": "G ", "BillingStreet ": "QTR"}"}';
I am facing issue when i am passing a json string and it is not getting deserialized as it is considering the whole json string as one value.
Here is my code and json string:
Code:
public override String abc(){
Map<String, Object> xyz = (Map<String, Object> )JSON.deserializeUntyped(jsonString);
Account acc = new Account();
system.debug('@@@@@'+xyz );
for (String key : xyz.keySet()){
String objString = (String)xyz.get(key);
if( key == 'Account'){
acc = (Account)JSON.deserialize(objString, Account.class);
}
}
jsonString:
'{"Account":"{ "Id ": "001550 ","ShippingStreet ": "abc ", "ShippingState ": "ON ", "ShippingCountry ": "A ", "ShippingCity ": "G ", "Phone ": "123456789 ", "BillingCountry ": "ABC ", "BillingCity ": "G ", "BillingStreet ": "QTR"}"}';
1. Your Json is not a valid one.
Please try the below valid json:
String jsonString = '{"Account": {"Id": "001550","ShippingStreet": "abc","ShippingState": "ON","ShippingCountry": "A","ShippingCity": "G","Phone": "123456789","BillingCountry": "ABC","BillingCity": "G","BillingStreet": "QTR"}}';
2. Update in apex:
public override String abc(){
Map<String, Object> xyz = (Map<String, Object> )JSON.deserializeUntyped(jsonString);
Account acc = new Account();
system.debug('@@@@@'+xyz );
for (String key : xyz.keySet()){
system.debug(xyz.get(key));
String objString = JSON.serialize(xyz.get(key));
if( key == 'Account')
acc = (Account)JSON.deserialize(objString, Account.class);
}
}
==========================
Execute the below code in Developer console or Workbench:
String jsonstr = '{"Account": {"Id": "001550","ShippingStreet": "abc","ShippingState": "ON","ShippingCountry": "A","ShippingCity": "G","Phone": "123456789","BillingCountry": "ABC","BillingCity": "G","BillingStreet": "QTR"}}';
system.debug( ' ==> ' + jsonstr );
Map<String, Object> xyz = (Map<String, Object> )JSON.deserializeUntyped(jsonstr );
Account acc = new Account();
system.debug('@@@@@'+xyz );
for (String key : xyz.keySet()){
system.debug(xyz.get(key));
String objString = JSON.serialize(xyz.get(key));
if( key == 'Account')
acc = (Account)JSON.deserialize(objString, Account.class);
}
system.debug( ' ===> ' + acc );
Thanks,
Maharajan.C
But now I am getting "Invalid conversion from runtime type List<ANY> to String" exception.