Closed PtrckGdwn closed 9 years ago
Hi Patrick,
please try the release of today. I have placed 2 new functions:
getValueAsJsonObject getValueAsString
with those, following code should help you. only what you need is to have second JSON object which you cna use as temporary storage.
Karol
JSONOBJECT_1.createFromValue("{\"customerId\": 1, \"customerName\": \"Customer A\",\"customerAddress\": { \"street\": \"1 Line One Street\",\"city\": \"Nowhereville\",\"state\": \"BestState\",\"postCode\" : null},\"customerPurchases\": [ { \"purchaseId\" : 1, \"productId\" : 231, \"productName\" : \"A JSON Book\", \"purchasePrice\" : 24.99},{ \"purchaseId\" : 2, \"productId\" : 365, \"productName\" : \"A JavaScript Book\", \"purchasePrice\" : 49.95} ],\"isLoyalCustomer\": false}");
// put the output into seconf JSON object JSONOBJECT_2.createFromJSON(JSONOBJECT_1.getValueAsJsonObject("customerAddress"));
// print full JSON TEXTAREA_1.setValue(JSONOBJECT_1.getAsJSON());
// read city from customer adress TEXTAREA_1.setValue(JSONOBJECT_2.getValueAsString("city") + "\r\n" + TEXTAREA_1.getValue());
// read customer id as string TEXTAREA_1.setValue(JSONOBJECT_1.getValueAsString("customerId") + "\r\n" + TEXTAREA_1.getValue());
Hi Karol,
I'm back working with DS after a long period working on other projects. Just wanted to let you know that I am using this component and it is working perfectly for me. Thank you for adding in these methods.
Cheers, Pat
I've been playing with the JSON utility, and one thing I would find very useful is to have the ability to retrieve a value by key.
Say I had the following JSON:
{ "customerId": 1, "customerName": "Customer A", "customerAddress": { "street": "1 Line One Street", "city": "Nowhereville", "state": "BestState", "postCode" : null },
"customerPurchases": [ { "purchaseId" : 1, "productId" : 231, "productName" : "A JSON Book", "purchasePrice" : 24.99 }, { "purchaseId" : 2, "productId" : 365, "productName" : "A JavaScript Book", "purchasePrice" : 49.95} ], "isLoyalCustomer": false }
With the above JSON I would like to be able to retrieve any value using its key. The return data type could be a string, an array, an integer, a float, a null or a JSON depending on what the returned value actually is.
For example, if we had an overloaded method "getValueByKey", then I would like to do the following:
1) Return the customerId as an integer (1). For example, something like: var customerId = JSONOBJECT_1.getValueByKey("customerId");
2) Return the customer's city ("Nowhereville"). For example, something like:
var customerCity = JSONOBJECT_1.getValueByKey("customerAddress").getValueByKey("city");
This example makes the assumption that can use dot notation to return a JSONOBJECT component, then return a string from that JSONOBJECT. I am not sure if that is possible considering that JSONOBJECT is a component, so perhaps some like this is more feasible (though less elegant):
JSONOBJECT_2.createFromJSON(JSONOBJECT_1.getValueByKey("customerAddress")); var customerCity = JSONOBJECT_2.getValueByKey("city");
3) Return the productName and purchasePrice of the customerPurchase with purchaseId 2 ("A JavaScript Book", 49.95)
I am not sure how to construct an example for this, mainly because I imagine it would involve looping and I am not so sure of Design Studio's/BIAL's support for looping on JSON objects. That being said, if you guys have a way to achieve this it would be truly useful.
Is it possible to add method(s) to the JSONOBJECT component to allow the above?