parsing bad json with a split and join - how

Former Member
Former Member
I am struggling to figure out how to bad some bad incoming json

Any suggestions?

The incoming "json" data comes in like

[4,"1.0",1434106807361]
[1,1434106891000]
[1,1434106934000]
[1,1434106995000]


and can't figure out the equivalent of this.


function(responseCode, data){

var json = $.parseJSON('['+data.split('\n').join(',')+']');

...

}

Is it possible?

thanks
(first time widget writer)

K
  • Assuming the web service you are contacting is returning a json response directly (not a string encoding some json data as a field within the json response), the json data that you get is already pre-parsed into MonkeyC array/dictionary objects. i.e., you should not need to parse any json.

    If you need help trying to figure out how to examine the data, it would be useful if you just provided a minimal (but complete) json snippet like you would get back if you pointed a web browser at the url.

    Travis
  • Former Member
    Former Member over 9 years ago
    Thanks Travis
    The returned "json" is not standard json. "The API is based on JSON, however the responses deviate away from the JSON standard. This is primarily to optimise the performance of the API."

    This is the complete return as indicated below (as returned via a web browser)

    [4,"1.0",1434106807361]
    [1,1434106891000]
    [1,1434106934000]
    [1,1434106995000]
    [1,1434106855000]
    [1,1434106908000]
    [1,1434107153000]


    When I do a makeJsonRequest method:onPosition

    function onPosition(responseCode,data) {
    var test = data.length();

    }

    var test=3 ie only the first line of 4, "1.0" and the 1434106807361 (this is epoch time)


    I can't test it in the simulator cause it crashed due to a known simulator bug. However if I debug by sending it to the watch (and it works fine then) I can get var test to display and data.toString() displays as [4,"1.0",1434106807361]

    I can never get to the second line I thought because of the newline at the end of each line. What I have read is that you treat each line as a separate JSON object. But not sure what that means nor how to march through it...
  • Former Member
    Former Member over 9 years ago
    The JSON request API is routed through Garmin Connect Mobile. GCM processes the responses from the server where the request is made and converts them into Monkey C types. Because of the way this process works, you will not be able to receive data that is not properly formatted JSON.

    To use this service, you may need to create a secondary service that can receive this request and reformat it.
  • Former Member
    Former Member over 9 years ago
    Darn.
    Well I learned alot at least. Thanks