You need to sign in to do that
Don't have an account?

Not able to get one (battery) of the JSON string value, getting other values but not battery. please help me.
Public Class JSONParseDisplay{
Public Static void ParseJSON() {
String Jsonstrn = '{"userDevices":[{"asserts":[" 2015-8-06 22:48:00"," 2015-8-06 22:48:00"],"battery":{"percentage":20,"voltage":3745},"deviceId":29308103,"deviceSerialNumber":"12EB5676534B","deviceType":"Charge HR","deviceWireId":"eb5672212b12","fwVersionRaw":"APP18.84 BSL18.84","hardwareVersion":"9","lastSyncClient":"api/sync-CLIENT-VISIBLE/mobile-ios/401","lastSyncedAt":"2015-08-10T13:27:56.000Z","pairedAt":"2015-04-10T19:51:15.000Z","pairedToEmail":"afit@somewhere.com"}]}';
//String Jsonstrn = '{"userDevices":[' +'{"asserts":[" 2015-8-06 22:48:00"," 2015-8-06 22:48:00"],"battery":[' +'{"percentage":20,"voltage":3745}],"deviceId":29308103,"deviceSerialNumber":"12EB5676534B","deviceType":"Charge HR","deviceWireId":"eb5672212b12","fwVersionRaw":"APP18.84 BSL18.84","hardwareVersion":"9","lastSyncClient":"api/sync-CLIENT-VISIBLE/mobile-ios/401","lastSyncedAt":"2015-08-10T13:27:56.000Z","pairedAt":"2015-04-10T19:51:15.000Z","pairedToEmail":"afitbitter@somewhere.com"}' +']}';
JSONParser parser = JSON.createParser(Jsonstrn);
System.debug('parser' +parser);
while (parser.nextToken() != null) {
if (parser.getCurrentToken() == JSONToken.START_ARRAY) {
while (parser.nextToken() != null) {
if (parser.getCurrentToken() == JSONToken.START_OBJECT) {
UserDevice usd = (UserDevice)parser.readValueAs(UserDevice.class);
System.debug('usd++ : ' +usd);
System.debug('pairedAt: ' +usd.pairedAt);
// System.debug('Size of batteries: ' +usd.batteries.size());
System.debug('batteries: ' +usd.batteries);
String s = JSON.serialize(usd);
System.debug('Serialized invoice: ' + s);
parser.skipChildren();
}
}
}
}
}
public class UserDevice {
List<string> asserts;
List<battery> batteries;
// List<string> batteries;
public Double deviceId;
public string deviceSerialNumber;
public string deviceType;
public string deviceWireId;
public string fwVersionRaw;
public Double hardwareVersion;
public string lastSyncClient;
public string lastSyncedAt;
public string pairedAt;
public string pairedToEmail;
public UserDevice(List<string> asser,List<battery> batt,Double devId,string deviceSerNumber,string devType,string devWireId,string fwVersRaw,Double hardVersion,string lastSyClient,string lastSyedAt,string paredAt,string paredToEmail){
asserts = asser;
batteries = batt.clone();
// batteries = Double.parseDouble(batt);
// batteries = batt;
// System.debug('batt.clone(): ' +batt.clone());
deviceId = devId;
deviceSerialNumber = deviceSerNumber;
deviceType = devType;
deviceWireId = devWireId;
fwVersionRaw = fwVersRaw;
hardwareVersion = hardVersion;
lastSyncClient = lastSyClient;
lastSyncedAt = lastSyedAt;
pairedAt = paredAt;
pairedToEmail = paredToEmail;
System.debug('pairedToEmail: ' +pairedToEmail);
}
}
public class battery {
public Double percentage;
public Double voltage;
//System.debug('voltage::' +voltage);
}
}
Public Static void ParseJSON() {
String Jsonstrn = '{"userDevices":[{"asserts":[" 2015-8-06 22:48:00"," 2015-8-06 22:48:00"],"battery":{"percentage":20,"voltage":3745},"deviceId":29308103,"deviceSerialNumber":"12EB5676534B","deviceType":"Charge HR","deviceWireId":"eb5672212b12","fwVersionRaw":"APP18.84 BSL18.84","hardwareVersion":"9","lastSyncClient":"api/sync-CLIENT-VISIBLE/mobile-ios/401","lastSyncedAt":"2015-08-10T13:27:56.000Z","pairedAt":"2015-04-10T19:51:15.000Z","pairedToEmail":"afit@somewhere.com"}]}';
//String Jsonstrn = '{"userDevices":[' +'{"asserts":[" 2015-8-06 22:48:00"," 2015-8-06 22:48:00"],"battery":[' +'{"percentage":20,"voltage":3745}],"deviceId":29308103,"deviceSerialNumber":"12EB5676534B","deviceType":"Charge HR","deviceWireId":"eb5672212b12","fwVersionRaw":"APP18.84 BSL18.84","hardwareVersion":"9","lastSyncClient":"api/sync-CLIENT-VISIBLE/mobile-ios/401","lastSyncedAt":"2015-08-10T13:27:56.000Z","pairedAt":"2015-04-10T19:51:15.000Z","pairedToEmail":"afitbitter@somewhere.com"}' +']}';
JSONParser parser = JSON.createParser(Jsonstrn);
System.debug('parser' +parser);
while (parser.nextToken() != null) {
if (parser.getCurrentToken() == JSONToken.START_ARRAY) {
while (parser.nextToken() != null) {
if (parser.getCurrentToken() == JSONToken.START_OBJECT) {
UserDevice usd = (UserDevice)parser.readValueAs(UserDevice.class);
System.debug('usd++ : ' +usd);
System.debug('pairedAt: ' +usd.pairedAt);
// System.debug('Size of batteries: ' +usd.batteries.size());
System.debug('batteries: ' +usd.batteries);
String s = JSON.serialize(usd);
System.debug('Serialized invoice: ' + s);
parser.skipChildren();
}
}
}
}
}
public class UserDevice {
List<string> asserts;
List<battery> batteries;
// List<string> batteries;
public Double deviceId;
public string deviceSerialNumber;
public string deviceType;
public string deviceWireId;
public string fwVersionRaw;
public Double hardwareVersion;
public string lastSyncClient;
public string lastSyncedAt;
public string pairedAt;
public string pairedToEmail;
public UserDevice(List<string> asser,List<battery> batt,Double devId,string deviceSerNumber,string devType,string devWireId,string fwVersRaw,Double hardVersion,string lastSyClient,string lastSyedAt,string paredAt,string paredToEmail){
asserts = asser;
batteries = batt.clone();
// batteries = Double.parseDouble(batt);
// batteries = batt;
// System.debug('batt.clone(): ' +batt.clone());
deviceId = devId;
deviceSerialNumber = deviceSerNumber;
deviceType = devType;
deviceWireId = devWireId;
fwVersionRaw = fwVersRaw;
hardwareVersion = hardVersion;
lastSyncClient = lastSyClient;
lastSyncedAt = lastSyedAt;
pairedAt = paredAt;
pairedToEmail = paredToEmail;
System.debug('pairedToEmail: ' +pairedToEmail);
}
}
public class battery {
public Double percentage;
public Double voltage;
//System.debug('voltage::' +voltage);
}
}
Please try with the above code. I hope this helps you.
All Answers
System.debug('usd++ : ' +usd);
DEBUG|usd++ : UserDevice:[asserts=( 2015-8-06 22:48:00, 2015-8-06 22:48:00), batteries=null, deviceId=2.9308103E7, deviceSerialNumber=12EB5676534B, deviceType=Charge HR, deviceWireId=eb5672212b12, fwVersionRaw=APP18.84 BSL18.84, hardwareVersion=9.0, lastSyncClient=api/sync-CLIENT-VISIBLE/mobile-ios/401, lastSyncedAt=2015-08-10T13:27:56.000Z, pairedAt=2015-04-10T19:51:15.000Z, pairedToEmail=afit@somewhere.com]
Please help me here.
Please try with the above code. I hope this helps you.
Once again Thanks alot for quick response
Regards,
Prashanth
It will be great help if replied back.
Thank you,
Prashanth
Hi, Please try something like above.
My household battery from a local company started leaking after three weeks and after time, it finally burst. Thanks to GOD everything was ok expect of little bit demages. But then i have decided i’ll never for a cheap company and used any product after a complete review. After 2 weaks of searching i have used a product named JBBATTERY that providing Best lithium ion golf cart battery pack manufacturer.
Forklift Battery Manufacturer (https://www.forkliftbatterymanufacturer.com/)