How to format JSON data without using brackets for an array?
To start with, this works (to be followed by what doesn’t):
// +++++ json
{ “cats”:
[
{
“pet” : “Jazzy”,
“about” : “Jazzy is a fast cat.”
},
{
“pet” : “Miss Kitty”,
“about” : “Miss Kitty loves catnip.”
}
]
}
// +++++ actionScript
package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.events.Event;
import com.adobe.serialization.json.JSON;
public class loadJSON extends Sprite {
private var _textField;
private var _textFormat;
private var _cat;
private var _urlRequest;
private var _textLoader;
public function loadJSON()
{
_urlRequest = new URLRequest( "jsonSample.txt" );
_textLoader = new URLLoader();
_textLoader.addEventListener( Event.COMPLETE, setText, false, 0, true );
_textLoader.load( _urlRequest );
}
private function setText( event:Event ):void
{
_cat = new Object();
_cat = JSON.decode( _textLoader.data );
_textField = new TextField();
_textField.text = _cat.cats[0].pet;
_textFormat = new TextFormat();
_textFormat.size = 20;
_textField.setTextFormat( _textFormat );
addChild( _textField );
}
}
}
When I format the JSON data and attempt to access it like so, I get a slew of JSONParseError(s)
// +++++ json
{ “cats”:
{ “myCat”:
{
“pet” : “Jazzy”,
“about” : “Jazzy is a fast cat.”
}
},
{ “momsCat”:
{
“pet” : “Miss Kitty”,
“about” : “Miss Kitty loves catnip.”
}
}
}
// +++++ actionScript
_textField.text = _cat.cats.myCat.pet;