0
{
  "ProductionOrder":"O000039",
  "statusType": "download",
      "Electronics/": [{
          "Fridges": [{
              "FileVar": {
                "Name": "Samsung",
                "currentext": ".jpeg",
                "Dirfilename": "/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/Age-Christoffer Lundeby_extra-large_t.jpg",
                "location": "a0eN0000000tELT",
                "filepreviewurl": "cifs://121.240.144.237:445/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/Age-Christoffer Lundeby_extra-large.jpeg",
                "filethumburl": "/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/Age-Christoffer Lundeby_extra-large_t.jpg",
                "orderid": "O000039",
                "filestatus": "TBI",
                "fileurl": "cifs://121.240.144.237:445/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/Age-Christoffer Lundeby_extra-large.jpeg"
              }
            },
            {
              "FileVar": {
                "Name": "LG",
                "currentext": ".jpg",
                "Dirfilename": "/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/Aqua_t.jpg",
                "location": "a0eN0000000tELT",
                "filepreviewurl": "cifs://121.240.144.237:445/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/Aqua.jpg",
                "filethumburl": "/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/Aqua_t.jpg",
                "orderid": "O000039",
                "filestatus": "TBI",
                "fileurl": "cifs://121.240.144.237:445/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/Aqua.jpg"
              }
            },
            {
              "FileVar": {
                "Name": "WhirlPool",
                "currentext": ".jpg",
                "Dirfilename": "/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/Blinds_t.jpg",
                "location": "a0eN0000000tELT",
                "filepreviewurl": "cifs://121.240.144.237:445/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/Blinds.jpg",
                "filethumburl": "/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/Blinds_t.jpg",
                "orderid": "O000039",
                "filestatus": "TBI",
                "fileurl": "cifs://121.240.144.237:445/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/Blinds.jpg"
              }
            },
            {
              "FileVar": {
                "Name": "SINGHT",
                "currentext": ".eps",
                "Dirfilename": "/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/heart_wings_t.jpg",
                "location": "a0eN0000000tELT",
                "filepreviewurl": "cifs://121.240.144.237:445/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/heart_wings.eps",
                "filethumburl": "/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/heart_wings_t.jpg",
                "orderid": "O000039",
                "filestatus": "TBI",
                "fileurl": "cifs://121.240.144.237:445/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/heart_wings.eps"
              }
            },
            {
              "Television/": [{
                  "FileVar": {
                    "Name": "SANSUI",
                    "currentext": ".jpg",
                    "Dirfilename": "/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/subFolder/Blinds_t.jpg",
                    "location": "a0eN0000000tELT",
                    "filepreviewurl": "cifs://121.240.144.237:445/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/subFolder/Blinds.jpg",
                    "filethumburl": "/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/subFolder/Blinds_t.jpg",
                    "orderid": "O000039",
                    "filestatus": "TBI",
                    "fileurl": "cifs://121.240.144.237:445/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/subFolder/Blinds.jpg"
                  }
                },
                {
                  "FileVar": {
                    "Name": "godrej",
                    "currentext": ".eps",
                    "Dirfilename": "/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/subFolder/heart_wings_t.jpg",
                    "location": "a0eN0000000tELT",
                    "filepreviewurl": "cifs://121.240.144.237:445/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/subFolder/heart_wings.eps",
                    "filethumburl": "/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/subFolder/heart_wings_t.jpg",
                    "orderid": "O000039",
                    "filestatus": "TBI",
                    "fileurl": "cifs://121.240.144.237:445/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/subFolder/heart_wings.eps"
                  }
                }]
            }]
        }]

}

and here FileVar is File__c Object Fields which i need to Insert into Salesforce .

AND MY CLASS WHERE I WAS STRUCK IS .. IN THE BELOW I CANT ABLE TO READ MY FIRST fILEvAR ..ITS SKIKPING FIRST

public list<FileVar> sample(JSONParser parser)
        {
            while (parser.nextToken() !=  JSONToken.END_ARRAY)  
            {  
                system.debug('entering into first while');
                if (parser.getCurrentToken() == JSONToken.START_OBJECT)  
                { 
                    FileVar fileVarObj = new FileVar ();     

            while (parser.nextToken() !=  JSONToken.END_OBJECT)  
            {  
                if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText() == 'Name'))  {  
                    parser.nextToken() ;      
                    fileVarObj.Name = parser.getText();  
                    System.debug('fileVarObj.Name===>' + fileVarObj.Name);
                }  
                if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'currentext'))  {  
                    parser.nextToken();  
                    fileVarObj.currentext = parser.getText();  
                    System.debug('fileVarObj.currentext===>' + fileVarObj.currentext);
                }  
                if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'Dirfilename'))  {  
                    parser.nextToken();  
                    fileVarObj.Dirfilename = parser.getText();
                    System.debug('fileVarObj.Dirfilename ===>' + fileVarObj.Dirfilename);  
                }  
                if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'location')) {  
                    parser.nextToken()     ;  
                    fileVarObj.location= parser.getText();
                    System.debug('fileVarObj.location ===>' + fileVarObj.Dirfilename);
                } 
                if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'filepreviewurl'))  {  
                    parser.nextToken()     ;  
                    fileVarObj.filepreviewurl= parser.getText();  
                }  
                if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'filethumburl'))  {  
                    parser.nextToken()     ;  
                    fileVarObj.filethumburl= parser.getText();  
                }  
                if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'orderid'))  {  
                    parser.nextToken()     ;  
                    fileVarObj.orderid= parser.getText();  
                }  
                if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'filestatus'))  {  
                    parser.nextToken()     ;  
                    fileVarObj.filestatus= parser.getText();  
                }  
                if((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText().tolowercase() == 'fileurl'))  {  
                    parser.nextToken()     ;  
                    fileVarObj.fileurl= parser.getText();  
                }

                System.debug('fileVarObj===>' + fileVarObj);   
            }  
                fileVarList.add(fileVarObj);  
            } System.debug(LoggingLevel.WARN, 'SubFolder1 consuming unrecognized property: ');
                EPMS_FileFielddetails.consumeObject(parser);  

            }
                 System.debug('fileVarList===>' + fileVarList);
                 System.debug('fileVar Size===>' + fileVarList.Size());

    return fileVarList;
        }  
  • Hi Wrick, if you can share your current code and highlight what you have tried so far in parsing this json and explain where you are stuck, it will help the community to help you.. Thanks. – Vamsi Krishna Jan 20 '16 at 5:24
  • Actually i can able read all the FileVar except First one...rest everything is creating Files ..i am posting my code as a Answer as it is too long – Wrick Jan 20 '16 at 13:46
  • You can use JSON.deserializeUntyped(String) to serialize to Map<String,Object> – Phil Hawthorn Jan 20 '16 at 14:47
  • its not possiable as it is dynamically nested JSON – Wrick Jan 21 '16 at 12:35
3
//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//

public class FileVarClass {

    public String ProductionOrder;
    public String statusType;
    public List<Electronics/> Electronics/;

    public class FileVar {
        public String Name;
        public String currentext;
        public String Dirfilename;
        public String location;
        public String filepreviewurl;
        public String filethumburl;
        public String orderid;
        public String filestatus;
        public String fileurl;
    }

    public class Fridges {
        public FileVar FileVar;
        public List<Fridges> Television/;
    }

    public class Electronics/ {
        public List<Fridges> Fridges;
    }


    public static FileVarClass parse(String json) {
        return (FileVarClass) System.JSON.deserialize(json, FileVarClass.class);
    }
}

These is very good tool JSON2APEX it will give you wrapper class of your json. So use this class and use the method parse and it will give you list of wrapper class and then use this list to insert the records.


Here is your test class

//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//

@IsTest
public class FileVarClass_Test {

    static testMethod void testParse() {
        String json = '{'+
        '  \"ProductionOrder\":\"O000039\",'+
        '  \"statusType\": \"download\",'+
        '      \"Electronics/\": [{'+
        '          \"Fridges\": [{'+
        '              \"FileVar\": {'+
        '                \"Name\": \"Samsung\",'+
        '                \"currentext\": \".jpeg\",'+
        '                \"Dirfilename\": \"/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/Age-Christoffer Lundeby_extra-large_t.jpg\",'+
        '                \"location\": \"a0eN0000000tELT\",'+
        '                \"filepreviewurl\": \"cifs://121.240.144.237:445/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/Age-Christoffer Lundeby_extra-large.jpeg\",'+
        '                \"filethumburl\": \"/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/Age-Christoffer Lundeby_extra-large_t.jpg\",'+
        '                \"orderid\": \"O000039\",'+
        '                \"filestatus\": \"TBI\",'+
        '                \"fileurl\": \"cifs://121.240.144.237:445/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/Age-Christoffer Lundeby_extra-large.jpeg\"'+
        '              }'+
        '            },'+
        '            {'+
        '              \"FileVar\": {'+
        '                \"Name\": \"LG\",'+
        '                \"currentext\": \".jpg\",'+
        '                \"Dirfilename\": \"/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/Aqua_t.jpg\",'+
        '                \"location\": \"a0eN0000000tELT\",'+
        '                \"filepreviewurl\": \"cifs://121.240.144.237:445/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/Aqua.jpg\",'+
        '                \"filethumburl\": \"/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/Aqua_t.jpg\",'+
        '                \"orderid\": \"O000039\",'+
        '                \"filestatus\": \"TBI\",'+
        '                \"fileurl\": \"cifs://121.240.144.237:445/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/Aqua.jpg\"'+
        '              }'+
        '            },'+
        '            {'+
        '              \"FileVar\": {'+
        '                \"Name\": \"WhirlPool\",'+
        '                \"currentext\": \".jpg\",'+
        '                \"Dirfilename\": \"/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/Blinds_t.jpg\",'+
        '                \"location\": \"a0eN0000000tELT\",'+
        '                \"filepreviewurl\": \"cifs://121.240.144.237:445/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/Blinds.jpg\",'+
        '                \"filethumburl\": \"/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/Blinds_t.jpg\",'+
        '                \"orderid\": \"O000039\",'+
        '                \"filestatus\": \"TBI\",'+
        '                \"fileurl\": \"cifs://121.240.144.237:445/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/Blinds.jpg\"'+
        '              }'+
        '            },'+
        '            {'+
        '              \"FileVar\": {'+
        '                \"Name\": \"SINGHT\",'+
        '                \"currentext\": \".eps\",'+
        '                \"Dirfilename\": \"/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/heart_wings_t.jpg\",'+
        '                \"location\": \"a0eN0000000tELT\",'+
        '                \"filepreviewurl\": \"cifs://121.240.144.237:445/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/heart_wings.eps\",'+
        '                \"filethumburl\": \"/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/heart_wings_t.jpg\",'+
        '                \"orderid\": \"O000039\",'+
        '                \"filestatus\": \"TBI\",'+
        '                \"fileurl\": \"cifs://121.240.144.237:445/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/heart_wings.eps\"'+
        '              }'+
        '            },'+
        '            {'+
        '              \"Television/\": [{'+
        '                  \"FileVar\": {'+
        '                    \"Name\": \"SANSUI\",'+
        '                    \"currentext\": \".jpg\",'+
        '                    \"Dirfilename\": \"/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/subFolder/Blinds_t.jpg\",'+
        '                    \"location\": \"a0eN0000000tELT\",'+
        '                    \"filepreviewurl\": \"cifs://121.240.144.237:445/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/subFolder/Blinds.jpg\",'+
        '                    \"filethumburl\": \"/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/subFolder/Blinds_t.jpg\",'+
        '                    \"orderid\": \"O000039\",'+
        '                    \"filestatus\": \"TBI\",'+
        '                    \"fileurl\": \"cifs://121.240.144.237:445/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/subFolder/Blinds.jpg\"'+
        '                  }'+
        '                },'+
        '                {'+
        '                  \"FileVar\": {'+
        '                    \"Name\": \"godrej\",'+
        '                    \"currentext\": \".eps\",'+
        '                    \"Dirfilename\": \"/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/subFolder/heart_wings_t.jpg\",'+
        '                    \"location\": \"a0eN0000000tELT\",'+
        '                    \"filepreviewurl\": \"cifs://121.240.144.237:445/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/subFolder/heart_wings.eps\",'+
        '                    \"filethumburl\": \"/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/subFolder/heart_wings_t.jpg\",'+
        '                    \"orderid\": \"O000039\",'+
        '                    \"filestatus\": \"TBI\",'+
        '                    \"fileurl\": \"cifs://121.240.144.237:445/share/JavaDev/Input/20151124/O000039/JAPAN/20151030/subFolder/heart_wings.eps\"'+
        '                  }'+
        '                }]'+
        '            }]'+
        '        }]'+
        ''+
        '}';
        FileVarClass obj = FileVarClass.parse(json);
        System.assert(obj != null);
    }
}`
  • hi tushar ,Json2apex will work when json string is constant but in this case might fews array will add to next request ..if the array has "\" we need to consider its is a directory for that array and Rest FileVar are Images ....and Images are child to Directories ..so that why is any way to deserailize using Maps<string,Object> – Wrick Jan 20 '16 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.