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
learning1.3953795511514497E12learning1.3953795511514497E12 

How to parse NestedJson in Apex

public class jsonPoarse
{
      public class AdressDetail
    {
        public String city{get;set;}
        public String postalCde{get;set;}
        public String Street{get;set;}
    }
   
    public class ContactDetail
    {
        public String fax{get;set;}
        public String phone{get;set;}
    }
   class companyDetails
    {
           public integer id{get;set;}
           public integer name{get;set;}
           // public Map<locations> {get;set;}
    }
public Static void nestedJsonParse()
    {
        String jsonInput = '{"companies": { "_count": 3, "_start": 0, "_total": 236, "values": [ { "id": 1318, "locations": { "_total": 3, "values": [ { "address": { "city": "Bangalore", "postalCode": "560035", "street1": "Dodda Kannelli" }, "contactInfo": { "fax": "", "phone1": "" } }, { "address": { "city": "Bangalore", "postalCode": "560100", "street1": "Electronics City" }, "contactInfo": { "fax": "", "phone1": "" } }, { "address": { "city": "Kochi", "postalCode": "682030", "street1": "Info Park Special Economic Zone" }, "contactInfo": { "fax": "", "phone1": "91-484-3054949" } } ] }, "name": "Wipro" }, { "id": 1315, "locations": { "_total": 5, "values": [ { "address": { "city": "Bangalore", "postalCode": "560 035", "street1": "Doddakannelli," }, "contactInfo": { "fax": "+91 (80) 28440256", "phone1": "+91 (80) 28440011" } }, { "address": { "city": "Corniche Road, Al Khobar,", "postalCode": "", "street1": "Suite 209, Level II," }, "contactInfo": { "fax": "", "phone1": "+966 (3) 898 4015" } }, { "address": { "city": "London", "postalCode": "W2 6PS", "street1": "West Wing, Level 2," }, "contactInfo": { "fax": "+44 207 286 5703", "phone1": "+44 207 432 8500" } }, { "address": { "city": "North Sydney,", "postalCode": "NSW - 2060", "street1": "Level 17," }, "contactInfo": { "fax": "+61 (2) 9394 8199", "phone1": "+61 (2) 9394 8100" } }, { "address": { "city": "Mississauga,", "postalCode": "L4W 4T9", "street1": "5090 Explorer Drive" }, "contactInfo": { "fax": "(905) 629-0956", "phone1": "(289) 374-2000" } } ] }, "name": "Wipro Infotech" }, { "id": 1314, "locations": { "_total": 3, "values": [ { "address": {}, "contactInfo": {} }, { "address": {"postalCode": "10167"}, "contactInfo": {} }, { "address": { "city": "East Brunswick", "postalCode": "08816", "street1": "2 Tower Center Blvd" }, "contactInfo": {} } ] }, "name": "Wipro Consulting" } ] }}';
        Map<String, Object> jsonInputdeserialized = (Map<String, Object>) JSON.deserializeUntyped(jsonInput);
        System.debug('jsonInputdeserialized =' +jsonInputdeserialized);
        System.debug('MapKeys = ' +jsonInputdeserialized.keySet());  
        System.debug('mapValues = ' +jsonInputdeserialized.get('companies'));
       
        Map<String,Object> CompaniesMap = (Map<String,Object>)jsonInputdeserialized.get('companies');
        System.debug('CompaniesMap = ' +CompaniesMap);
        System.debug('CompaniesMap keys = ' +CompaniesMap.keySet());
        System.debug('CompaniesMap values = ' +CompaniesMap.get('values'));
       
//        Map<String,Object> valuesMap = (Map<String,Object>)CompaniesMap.get('values');

*********
Here I want to get the values of address,contactinfo , and total locations

******************


       
       
       
    }
}
Best Answer chosen by learning1.3953795511514497E12
learning1.3953795511514497E12learning1.3953795511514497E12
For Time being I am replacing the fields sarting with underscore with linkedin_<fieldname> and crating the wrapper classes,
eg : _count is replaced by linkedin_count,
Still any help would be appreciated.

All Answers

Abhi_TripathiAbhi_Tripathi
Hi there,

1. first of all create a wrapper class,
2. take variable in wrapper same as in JSON,
3. In the main controller class, create a list of Wrapper class LiST<WrapperClass>.
4. Append you JSON in wrapper class list
4. Then bind that wrapper list on page , you can see all the values of the variables of the JSON.

For better understanding read this post
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_json_jsonparser.htm

Here is you JSON, this will make you easy to understand
{
	"companies":
	{ 
		"_count": 3,
		"_start": 0,
		"_total": 236,
		"values":
		[
			{
			"id": 1318,
			"locations": 
				{
				"_total": 3,
				"values":
				[
					{
					"address":
					{
						"city": "Bangalore",
						"postalCode": "560035",
						"street1": "Dodda Kannelli" 
					},
					"contactInfo": 
					{
						"fax": "", "phone1": "" 
					}
				}, { 
					"address": 
					{ 
						"city": "Bangalore",
						"postalCode": "560100",
						"street1": "Electronics City" 
					}, 
					"contactInfo": 
					{ 
					"fax": "",
					"phone1": "" 
					} 
				}, {
					"address":
					{ 
						"city": "Kochi",
						"postalCode": "682030",
						"street1": "Info Park Special Economic Zone"
					},
					"contactInfo": 
					{ 
						"fax": "",
						"phone1": "91-484-3054949"
					}
				} ]
			},
			"name": "Wipro"
		}, {
			"id": 1315,
			"locations":
			{
				"_total": 5,
				"values": 
				[ {
					"address": 
					{
						"city": "Bangalore",
						"postalCode": "560 035",
						"street1": "Doddakannelli,"
					}, 
					"contactInfo":
					{
						"fax": "+91 (80) 28440256",
						"phone1": "+91 (80) 28440011"
					}
				},{
					"address":
					{
						"city": "Corniche Road, Al Khobar,",
						"postalCode": "", 
						"street1": "Suite 209, Level II,"
					}, 
					"contactInfo": 
					{ 
						"fax": "",
						"phone1": "+966 (3) 898 4015" 
					} 
				}, {
					"address":
					{ 
						"city": "London",
						"postalCode": "W2 6PS",
						"street1": "West Wing, Level 2,"
					},
					"contactInfo": 
					{
						"fax": "+44 207 286 5703",
						"phone1": "+44 207 432 8500"
					} 
				},{ 
					"address": 
					{ 
						"city": "North Sydney,",
						"postalCode": "NSW - 2060",
						"street1": "Level 17,"
					},
					"contactInfo":
					{
						"fax": "+61 (2) 9394 8199",
						"phone1": "+61 (2) 9394 8100" 
					}
				}, { 
					"address": 
					{ 
						"city": "Mississauga,",
						"postalCode": "L4W 4T9",
						"street1": "5090 Explorer Drive"
					}, 
					"contactInfo": 
					{
						"fax": "(905) 629-0956",
						"phone1": "(289) 374-2000"
					}
				}] },
					"name": "Wipro Infotech"
				}, { 
					"id": 1314,
					"locations": 
					{
						"_total": 3,
						"values": 
						[{ 
							"address": {},
							"contactInfo": {}
						}, {
						"address": 
						{
							"postalCode": "10167"
						}, 
						"contactInfo": {}
					}, { 
						"address": 
						{ 
							"city": "East Brunswick",
							"postalCode": "08816",
							"street1": "2 Tower Center Blvd"
						}, 
						"contactInfo": {}
					} ]
				}, 
				"name": "Wipro Consulting"
			} 
		]
	}
}';

Regards,
Abhi Tripathi
Salesforce Certified Developer
Join My Blog : http://abhithetechknight.blogspot.in/

learning1.3953795511514497E12learning1.3953795511514497E12
Hi Abhi,

Thanks for the reply,

If you see to the Json String which I am getting as linkedin reaponse, There are many fields starting with underscore, like _count,_total etc..
so I cannot create the String starting with underscore in the wrapper class, also replacing underscore with someother string is not a good practice here as it may effect the other string too. this is the exact problem I am facing.
Is there any alternative solution or is salesforce looking for any solutions which ables us to create string starting with underscore?

learning1.3953795511514497E12learning1.3953795511514497E12
For Time being I am replacing the fields sarting with underscore with linkedin_<fieldname> and crating the wrapper classes,
eg : _count is replaced by linkedin_count,
Still any help would be appreciated.
This was selected as the best answer