1
public class fromJSON{
   public cls_Student[] Student;
   class cls_Student {
       public String Name; //Vinayak
       public String branch;   //Mech
       public String phone;    //909090
   }
   public static fromJSON parse(String json){
       return (fromJSON) System.JSON.deserialize(json, fromJSON.class);
   } 
}

I am trying to print json object into debug log=>

//This is anonymous window code to execute above json.
String js='{"Name":"Vinayak","branch":"Mech","phone":"909090"}';
System.debug(fromJSON.parse(js));

Getting this result in debug logs => 12:01:17:019 USER_DEBUG [2]|DEBUG|fromJSON:[Student=null]

2 Answers 2

4

Assuming your requirement is to have an array of students having names, branches, and phones. JSON shall be structured below as

{"students":[
   {"Name": "Vinayak", "branch": "Mech", "phone": "909090"},
   {"Name": "Vinayak 2", "branch": "Mech", "phone": "909091"},
   .
   .
   .
   so on and so forth

]}

In this scenario, your class structure will be

public Class Student{
           public String name; 
           public String branch;   
           public String phone;    
    }

and top-level class will be

public Class StudentsList{
       public List<Student> students; 
}

and to parse the JSON string you can use

StudentsList students = (StudentsList)System.JSON.deserialize(json, StudentsList.class);
1
  • Thanks....this is what i want...thank u so much Jun 12, 2022 at 12:28
6

Your JSON doesn't match your Apex. Your Apex class represents a JSON structure like this:

{
    "Student": [{"Name": "Vinayak", "branch": "Mech", "phone": "909090"}]
}

Note the extra level of structure under the key "Student".

Either the JSON example or the Apex is wrong, but I couldn't say which one from the details given in your question. See How do I get started working with JSON in Apex? for introduction and examples.

1
  • Thanks David for ur response... Jun 12, 2022 at 12:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .