How to parse String with XML or JSON inside?
And how to create XML/JSON string?
Any ideas...
//var headers = {
// "Content-Type" => Comm.REQUEST_CONTENT_TYPE_JSON
//};
var options = {
//:method => Comm.HTTP_REQUEST_METHOD_GET,
//:headers => headers,
:responseType => Comm.HTTP_RESPONSE_CONTENT_TYPE_JSON
};
var params = {
};
var url = "jsonplaceholder.typicode.com/.../1";
Comm.makeWebRequest(url, params, options, method(:onWebResponse));
function onWebResponse(code, data) {
Sys.println(code);
Sys.println(data); // this is the JSON data as a Lang.Dictionary
}
var object = {
"1" => [ 1, 2, 3 ],
2 => "Hello World!"
};
Communications.transmit(object, params, new ConnectionListener());
function onMail(mailIterator) {
var mail = mailIterator.next();
while (mail != null) {
// if you need to know what the type of the object was, you can use
// instanceof to tell. personally, i think it is better to always send a
// dictionary.
// assume the mobile app is echoing the message we sent above
Sys.println(mail)
Sys.println(mail["1"][0]); // should print 1
Sys.println(object[2]); // should print "Hello World!"
mail = mailIterator.next();
}
}
I'm still not sure I understand the need to format your message data as XML/JSON. When communicating between a mobile app and a MonkeyC app on the device, you use Communications.transmit() to send an object to the mobile device.. it doesn't need to be a String. You just send an object, which I believe could be a String, Array, Dictionary, Number, Float...
<message>
<ref>5431<ref>
<run>1<run>
<type>Apply<type>
<results>
<targets>
<target>1<target>
<target>2<target>
</targets>
<values>
<value>0<value>
<value>15<value>
</targets>
</results>
</message>
If you have a JSON/XML string on the mobile
It seems to me that the better solution would be to convert that string into an object representation, and then send that.
// if you need to know what the type of the object was, you can use
// instanceof to tell. personally, i think it is better to always send a
// dictionary.
This data cannot be easy represented as Monkey C object.
var object = {
"message" => {
"ref" => 5431,
"run" => 1,
"results" => {
"targets" => [
1,
2
],
"values" => [
0,
15
]
}
}
};
Maybe it can be some combination of Dictionary, Arrays and primitives.
But it is not clear what will be received inside mobile application. Is it JSONObject?
You can send messages to your Connect IQ application on a connected device using any of the Java equivalent Monkey C data types (see Supported Data Types table below). Calling sendMessage() will deliver the message to your applications mailbox.
Supported Data Types
Java Data Type — Monkey C Type
int, Integer — Integer
float, Float — Float
boolean, Boolean — Boolean
String — String
List<?> — Array[] — Note: List must contain only supported data types. If the list contains unsupported data types, an exception will be thrown.
Map<?,?> — Dictionary — Note: Map keys and values must be supported data types. If the map contains unsupported data types, an exception will be thrown.