Populating the new custom data for Product

In this lesson, you extend the JSON parsing code wherever the POJOs are created to read the userData from the JSON object and set it back on the POJO.

About this task

In the Android native application, the classes for handling REST services requests and responses are in the com.ibm.commerce.android.nativeapp.mobile.utils package:
  1. In the Package Explorer view of Eclipse, open the file src > com.ibm.commerce.android.nativeapp.mobile.utils > JSONParser.java
  2. Locate the getProductDetailsFromJSON method. This method populates the Product object when you navigate to the product display page in the store. Scroll to the first try block and find the following line:
    productDetails.setName(productObj.getString(ApplicationConstants.NAME_TAG));

    Copy the following code right before the line that you found:

    			Map<String, String> userdata = this.getUserData("CatalogEntryView[0]");
    			productDetails.setWarrantyTerm(userdata.get("xcatentry_WARRANTYTERM"));
    			productDetails.setWarrantyType(userdata.get("xcatentry_WARRANTYTYPE"));

    As you may notice that in wc-business-mediator.xml, we define the user data at catalogEntryView level within the CatalogNavigationView noun.

    
    <_config:mediator-property 
    name="CatalogEntryView/UserData[(Name='WARRANTYTERM')]" 
    value="warterm" />
    
    <_config:mediator-property 
    name="CatalogEntryView/UserData[(Name='WARRANTYTYPE')]" 
    value="wartype" />

    From the poster response, you can see that user data is returned by REST service as xcatentry_WARRANTYTERM and xcatentry_WARRANTYTYPE. The user data is not directly present in the top level of the JSON object returned by REST. Therefore, you must use the JSON path to get the user data map for the corresponding JSON object. CatalogEntryView is returned as an array under the top-level JSON object returned by the REST service. Therefore, if you need to access the userdata in the first catentry in the array, then your JSON path would be: CatalogEntryView[0]. If you need to access userdata defined in the top-level JSON object, then you can use the method getUserdata().

  3. Save and close the file.

Results