I have experience with JSON parsing in Android and often come across responses like the one below:
{
"contacts": [
{
"id": "c200",
"name": "Ravi Tamada",
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b7c5d6c1def7d0d6dedb99d4d8da">[email protected]</a>",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c201",
"name": "Johnny Depp",
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d2b8bdbabcbcab8db6b7a2a292b5bfb3bbbefcb1bdbf">[email protected]</a>",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
]
}
When working with this type of response, we typically extract the data as shown below:
JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray contacts = jsonObj.getJSONArray("contacts");
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String id = c.getString("id");
String name = c.getString("name");
String email = c.getString("email");
String address = c.getString("address");
String gender = c.getString("gender");
JSONObject phone = c.getJSONObject("phone");
String mobile = phone.getString("mobile");
String home = phone.getString("home");
String office = phone.getString("office");
}
}
Now, I am exploring how to achieve the same data extraction in TypeScript using Angular2. As a beginner in Angular2, I would appreciate any guidance on this matter. Thank you.