У меня проблемы с получением кода для эффективного получения значений.
{
"Members": {
"Member": {
"@First_Id": "000",
"@Second_Id": "000",
"@Third_Id": "000",
"@Fourth_Id": "000",
"DisplayAs": "Title FirstName LastName",
"ListAs": "LastName, Title FirstName",
"FullTitle": "Title Title FirstName LastName Title",
"PreviousName": null,
"DateOfBirth": "2020-01-01T00:00:00",
"DateOfDeath": {
"@xsi:nil": "true",
"@xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance"
},
"Gender": "M OR F",
"Party": {
"@Id": "4",
"#text": "Party Name"
},
"House": "House Name",
"MemberFrom": "Area Name",
"HouseStartDate": "2020-01-01T00:00:00",
"HouseEndDate": {
"@xsi:nil": "true",
"@xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance"
},
"CurrentStatus": {
"@Id": "0",
"@IsActive": "True",
"Name": "Current Member",
"Reason": null,
"StartDate": "2020-01-01T00:00:00"
},
"Addresses": {
"Address": [
{
"@Type_Id": "6",
"Type": "Website",
"IsPreferred": "False",
"IsPhysical": "False",
"Note": null,
"Address1": "https://www.example.co.uk"
},
{
"@Type_Id": "1",
"Type": "Address1",
"IsPreferred": "False",
"IsPhysical": "True",
"Note": null,
"Address1": "Line 1",
"Address2": "Line 2",
"Address3": "Line 3",
"Address4": null,
"Address5": "County Name",
"Postcode": "Postcode",
"Phone": "000 0000 000",
"Fax": "000 0000 000",
"Email": "example@email.co.uk",
"OtherAddress": null
},
{
"@Type_Id": "7",
"Type": "Twitter",
"IsPreferred": "False",
"IsPhysical": "False",
"Note": null,
"Address1": "https://twitter.com/twitter_name"
},
{
"@Type_Id": "4",
"Type": "Address2",
"IsPreferred": "False",
"IsPhysical": "True",
"Note": null,
"Address1": "Line1",
"Address2": "Line2",
"Address3": "Line3",
"Address4": "Line4",
"Address5": null,
"Postcode": "Postcode",
"Phone": "0000 000 000",
"Fax": null,
"Email": "example2@email.co.uk",
"OtherAddress": null
}
]
},
"PrimaryPosts": {
"PrimaryPost": [
{
"@Id": "000",
"Name": "Name",
"ProperName": "The Name",
"StartDate": "2020-01-01T00:00:00",
"EndDate": "2020-01-01T00:00:00",
"Note": null,
"EndNote": null,
"IsJoint": "False",
"IsUnpaid": "False",
"Email": null,
"FormerName": "Name"
},
{
"@Id": "000",
"Name": "Name",
"ProperName": "The Name",
"StartDate": "2020-01-01T00:00:00",
"EndDate": "2020-01-01T00:00:00",
"Note": null,
"EndNote": null,
"IsJoint": "False",
"IsUnpaid": "False",
"Email": null,
"FormerName": "Name"
}
]
},
"FormerPosts": {
"FormerPost": [
{
"@Id": "000",
"Name": "Name",
"ProperName": "The Name",
"StartDate": "2020-01-01T00:00:00",
"EndDate": "2020-01-01T00:00:00",
"Note": null,
"EndNote": null,
"IsJoint": "False",
"IsUnpaid": "False",
"Email": null
},
{
"@Id": "000",
"Name": "Name",
"ProperName": "The Name",
"StartDate": "2020-01-01T00:00:00",
"EndDate": "2020-01-01T00:00:00",
"Note": null,
"EndNote": null,
"IsJoint": "False",
"IsUnpaid": "False",
"Email": null
}
]
},
"BiographyEntries": {
"BiographyEntry": [
{
"@Category_Id": "1",
"Category": "Interest 1",
"Entry": "Value1, Value2, Value3, Value4"
},
{
"@Category_Id": "2",
"Category": "Interest 2",
"Entry": "Value1, Value2, Value3, Value4"
}
]
}
}
}
}
До сих пор я был в состоянии проанализировать большую часть данных, но думаю, что есть лучший способ извлечь их без получения грузы ошибочной информации. Есть ли способ проанализировать информацию в массивах Json путем идеализации их @Type_Id?
Например, можно вернуть только телефон, URL-адрес Twitter, URL-адрес веб-сайта и адрес электронной почты, указав идентификатор типа?
private void jsonParse() {
final String postcode = userPostcode.getText().toString().trim();
String lookupUrl = "https://example/example/example" + postcode + "example/example/example";
final String imageURL = "https://example/example/example";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, lookupUrl, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
JSONObject members = null;
try {
members = response.getJSONObject("Members");
JSONObject member = members.getJSONObject("Member");
String firstID = String.valueOf(member.getInt("@First_Id"));
String areaName = member.getString("MemberFrom");
String name = member.getString("DisplayAs");
String partyName = partyOf.getString("#text");
String mpGender = member.getString("Gender");
String mpDOB = member.getString("DateOfBirth");
String mpFrom = member.getString("HouseStartDate");
String mpImageURL = imageURL + mpID + "/";
JSONObject partyOf = member.getJSONObject("Party");
JSONObject addresses = member.getJSONObject("Addresses");
JSONArray addressesArray = addresses.getJSONArray("Address");
for (int i = 0; i < addressesArray.length(); i++) {
try {
JSONObject addressObjects = addressesArray.getJSONObject(i);
} catch (JSONException e) {
e.printStackTrace();
}
}
JSONObject primaryPosts = member.getJSONObject("PrimaryPosts");
JSONArray primaryPostsArray = primaryPosts.getJSONArray("PrimaryPost");
for (int i = 0; i < primaryPostsArray.length(); i++) {
try {
JSONObject primiaryPostObjects = primaryPostsArray.getJSONObject(i);
} catch (JSONException e) {
e.printStackTrace();
}
}
JSONObject formerPosts = member.getJSONObject("FormerPosts");
JSONArray formerPostsArray = formerPosts.getJSONArray("FormerPost");
for (int i = 0; i < formerPostsArray.length(); i++) {
try {
JSONObject formerPostsObject = formerPostsArray.getJSONObject(i);
} catch (JSONException e) {
e.printStackTrace();
}
}
JSONObject biographyEntries = member.getJSONObject("BiographyEntries");
JSONArray BiographyEntriesArray = biographyEntries.getJSONArray("BiographyEntry");
for (int i = 0; i < BiographyEntriesArray.length(); i++) {
try {
JSONObject biographyEntriesPostsObject = BiographyEntriesArray.getJSONObject(i);
} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Error", "onErrorResponse: " + error.getMessage());
error.printStackTrace();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json");
return params;
}
};
mQueue.add(request);
}