-1

I'm having difficulty getting my code to get the values efficiently.


    {
    "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"
    }
    ]
    }
    }
    }
    }

So far I have been able to parse most of the data but think there is a better way to extract it without getting loads of erranous information. Is there a way to parse the information within the Json arrays by idetifiying their @Type_Id?

For example only return the phone, Twitter URL, website URL and email by looking for their type id?


    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);
    }

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437

1 Answers1

0

To not get overwhelmed by the flux of data and gain in code clarity, you could use this kind of website in order to generate your java models : jsonschema2pojo

You then just need to paste your JSON code in it, enter your package name and choose JSON inside the 'Source Type', then you have the choice to either download the Java models in a zip or view directly the result.

If you get this JSON from a network call, you should consider to try the Retrofit library instead, it's a lot more simpler to parse data with it (with Retrofit Scalar-Converter, you can even have the parsing done automatically and get a String directly in the response).

Retrofit link

I hope this will be helpful to you, let us know how things went.

  • Thanks VeryDarkCode i have implemented retrofit but still have the same issue when trying to extract the specific string from the dataset. below is the link to the new question (sorry new to stackoverflow so not sure this is the correct way of following up) https://stackoverflow.com/questions/60002782/how-to-retrieve-specific-data-from-json-array-via-retrofit2-using-gson-parsing – NewToCoding Jan 31 '20 at 11:16