function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Prashanth Kumar Singh Poorna SinghPrashanth Kumar Singh Poorna Singh 

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);     
 
 
 } 


}
Best Answer chosen by Prashanth Kumar Singh Poorna Singh
Muthuraj TMuthuraj T
public class UserDevice{
    List<UserDevices> userDevices;
} 
public class UserDevices {
        
        List<string> asserts;
        public battery battery;
        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 class battery {
        public Double percentage;
        public Double voltage;
 } 

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"}]}';

UserDevice ud = (UserDevice)JSON.deserialize(Jsonstrn,UserDevice.class);
system.debug(':::'+ud);
system.debug('Battery::'+ud.UserDevices[0].battery);



Output:

|DEBUG|:::UserDevice:[userDevices=(UserDevices:[asserts=( 2015-8-06 22:48:00,  2015-8-06 22:48:00), battery=battery:[percentage=20.0, voltage=3745.0], 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])]

|DEBUG|Battery::battery:[percentage=20.0, voltage=3745.0]

Please try with the above code. I hope this helps you.

All Answers

Prashanth Kumar Singh Poorna SinghPrashanth Kumar Singh Poorna Singh
In the above code, I am trying to deserialize the JSON string using readValueAs , Its working fine getting all values but except battery from JSON string, "battery":{"percentage":20,"voltage":3745}, Below is the system debug log value I am getting, you can see batteries is null.
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. 

 
Muthuraj TMuthuraj T
public class UserDevice{
    List<UserDevices> userDevices;
} 
public class UserDevices {
        
        List<string> asserts;
        public battery battery;
        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 class battery {
        public Double percentage;
        public Double voltage;
 } 

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"}]}';

UserDevice ud = (UserDevice)JSON.deserialize(Jsonstrn,UserDevice.class);
system.debug(':::'+ud);
system.debug('Battery::'+ud.UserDevices[0].battery);



Output:

|DEBUG|:::UserDevice:[userDevices=(UserDevices:[asserts=( 2015-8-06 22:48:00,  2015-8-06 22:48:00), battery=battery:[percentage=20.0, voltage=3745.0], 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])]

|DEBUG|Battery::battery:[percentage=20.0, voltage=3745.0]

Please try with the above code. I hope this helps you.
This was selected as the best answer
Prashanth Kumar Singh Poorna SinghPrashanth Kumar Singh Poorna Singh
Thanks alot Muthuraj T ! It was very useful and quick, My code is working after this changes. 
Once again Thanks alot for quick response

Regards,
Prashanth
Prashanth Kumar Singh Poorna SinghPrashanth Kumar Singh Poorna Singh
I was able to get the values on debug Log but not able to print it on VF page. I tried passing the value to VF page but not able to do it. Can you please help me to print "battery" and "pairedAt" values on the VFpage.

It will be great help if replied back. 

Thank you,
Prashanth
Muthuraj TMuthuraj T
Controller:::

public class ParseJson{

    public battery batteryObj {get; set;}
    public String pairedAt {get; set;}

public class UserDevice{
    List<UserDevices> userDevices {get; set;}
} 
public class UserDevices {
        
        List<string> asserts;
        public battery battery;
        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 class battery {
        public Double percentage {get; set;}
        public Double voltage {get; set;}
 } 
 public ParseJson(){
     batteryObj = new Battery(); 
 }
 public void sampleMethod(){
    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"}]}';
    
    UserDevice userDeviceObj = (UserDevice)JSON.deserialize(Jsonstrn,UserDevice.class);
    system.debug(':::'+userDeviceObj );
    system.debug('Battery::'+userDeviceObj.UserDevices[0].battery);
    batteryObj  = userDeviceObj.UserDevices[0].battery;
    pairedAt = userDeviceObj.UserDevices[0].pairedAt;
 }


}

******************************************************************************
Page:

<apex:page controller="ParseJson" action="{!sampleMethod}">
{!batteryObj} <br />
{!pairedAt}
</apex:page>

Hi, Please try something like above.
batteryre batteryrebatteryre batteryre
Once purchased with Best car batteries with amazing CCA, you will be going to witness the good amount of outcome. If you are really looking forward to buy, then choose the right platform and get it.
tech updatetech update
Hi,

 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. 


 
Jonye KeeJonye Kee
We especially offer a wide range of lithium iron Phosphate (LiFePO4) batteries for electric forklift truck, each specifically engineered to deliver a high cycle life and excellent performance over a wide operating temperature.
Forklift Battery Manufacturer (https://www.forkliftbatterymanufacturer.com/)