Search code examples
stackmob

how to parse SMValue to String array


I am just getting familiar with the StackMob server side custom code sdk and i am trying to get a relationship field and iterate through it in the form of a String array. How do i do that ? is it possble to iterate through it without parsing it into an array?

           DataService ds = serviceProvider.getDataService();
           List<SMCondition> query = new ArrayList<SMCondition>();
           query.add(new SMEquals("product_id", new SMString(jsonObj.getString("product_id"))));
           List<SMObject> results = ds.readObjects("product", query);
           SMObject product= results.get(0); 
           //product.getValue().get("categories"); how do i get this to be a String array?

Solution

  • At its simplest, that would look something like this:

    List<SMValue> categories = (List<SMValue>)(rawObj.getValue().get("categories").getValue());
    for (SMValue smString : categories) {
        SMString stringValue = (SMString)smString.getValue();
        //do whatever you want with the string value here
    }
    

    Obviously there are some unchecked casts in here, so you will want to add type/null checking to the appropriate sections depending on your data & schema.