+ Start a Discussion
salesforcequestions123salesforcequestions123 

parsing sting urgent

hi this is the format i am getting response can u parse this format and help me
{
"Success": "true",
"Assessments": [
"AssessmentID": "123456",
"AssessmentName": "Demo Assessment",
"AssessmentType": "Test",
"CostPoints": "1",
"NumberOfQuestions": "35",
"TimeLimit": "2100"
]
}
to retrieve the 6 values only when success is true

AdrianCCAdrianCC
salesforcequestions123salesforcequestions123

Hi

 

Actually my json string is not in standard format to json parse it.How can i onvert the string with tkens do i can acess individual elements like accesseries id,aand others

Noam.dganiNoam.dgani

Hi

 

The incorrect json part of your string is that the array elements should be engulfed in curly bracets, as such:

 

{
"Success": "true",
"Assessments": [
{"AssessmentID": "123456"},
{"AssessmentName": "Demo Assessment"},
{"AssessmentType": "Test"},
{"CostPoints": "1"},
{"NumberOfQuestions": "35"},
{"TimeLimit": "2100"}
]

 

if you can get the response in proper json like above, then you can use the following class and the standard force.com JSON parser:

 

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

public class JSON2Apex {

	public class Assessments {
		public String AssessmentID;
		public String AssessmentName;
		public String AssessmentType;
		public String CostPoints;
		public String NumberOfQuestions;
		public String TimeLimit;
	}

	public String Success;
	public List<Assessments> Assessments;

	
	public static JSON2Apex parse(String json) {
		return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
	}
	
	static testMethod void testParse() {
		String json = '{'+
		'\"Success\": \"true\",'+
		'\"Assessments\": ['+
		'{\"AssessmentID\": \"123456\"},'+
		'{\"AssessmentName\": \"Demo Assessment\"},'+
		'{\"AssessmentType\": \"Test\"},'+
		'{\"CostPoints\": \"1\"},'+
		'{\"NumberOfQuestions\": \"35\"},'+
		'{\"TimeLimit\": \"2100\"}'+
		']'+
		'}';
		JSON2Apex obj = parse(json);
		System.assert(obj != null);
	}
}

 

Cheers