Richard
Richard

Reputation: 1

appcelerator titanium cannot parse JSON

I'm new to titanium and get difficulty in parsing JSON from mysql export. the json is valid and I feel frustrated with many unsuccessful trials. To simplify the code, I put it below. The code just stop and said: [ERROR] Script Error = Unable to parse JSON string

var win = Titanium.UI.currentWindow;


var hotdealjson = "{'hotdeal':[{'place':'bangkok','date':'4D3N','cost':'$4999up'},{'place':'tokyo','date':'3D2N','cost':'$3799up'}]}";


//read json
var response = JSON.parse(hotdealjson);
alert(response.hotdeal.length);

Thanks & regards, Richard

Upvotes: 0

Views: 5905

Answers (3)

Manaday Mavani
Manaday Mavani

Reputation: 123

It's always better to check the JSON validity before using/parsing anywhere! Use the JSONLint - The JSON validator for the same.

Upvotes: 0

Dogweather
Dogweather

Reputation: 16779

Here's what works for me:

var object = eval('(' + json_text + ')');

That's from the Kitchen Sink, and I've seen that evaluation style advocated in a Javascript text.

Upvotes: 0

Rich
Rich

Reputation: 7214

The JSON is actually invalid. The single quotes should be double quotes.

A very common mistake.

Upvotes: 5

Related Questions