Viewed   128 times

I am getting "Error #1132: Invalid JSON parse input" and cannot understand why.

My json is generated by php: json_encode($x). Output json if displayed in TextArea(flex) shows this:

{
   "title":"The Incredibles",
   "year":"2004",
   "type":"movie",
   "id":"9806",
   "imdb_id":"tt0317705",
   "rating":8.6,
   "tagline":"No gut, no glory",
   "overview":"Bob Parr has given up his superhero days to log in time as an insurance adjuster and raise his three children with his formerly heroic wife in suburbia. But when he receives a mysterious assignment, it\'s time to get back into costume.",
   "runtime":115,
   "budget":92000000,
   "image":"http://cf2.imgobject.com/t/p/w185/jjAgMfj0TAPvdC8E5AqDm2BBeYz.jpg",
   "trailer":"rMfrFG_69zM"
}

I validated with several validators and all of them say it's valid json.

On the flex side I am trying to access json with this code:

JSON.parse(event.result.toString());

but get the error. Has anyone had this problem?

Edit 1:

It seems that the overview line is where the issue is but I dont understand why exactly since I used php json_encode which should escape things correctly...

 Answers

1

The escape sequence of \' appears to terminate the JSON.

it\'s should be it's if you want "it's".

Since this JSON uses " for strings, it could just be: it's.

JSON:

{
   "title":"The Incredibles",
   "year":"2004",
   "type":"movie",
   "id":"9806",
   "imdb_id":"tt0317705",
   "rating":8.6,
   "tagline":"No gut, no glory",
   "overview":"Bob Parr has given up his superhero days to log in time as an insurance adjuster and raise his three children with his formerly heroic wife in suburbia. But when he receives a mysterious assignment, it's time to get back into costume.",
   "runtime":115,
   "budget":92000000,
   "image":"http://cf2.imgobject.com/t/p/w185/jjAgMfj0TAPvdC8E5AqDm2BBeYz.jpg",
   "trailer":"rMfrFG_69zM"
}
Wednesday, December 21, 2022
 
4

I would use xml for something like this, but it can be done with url encoded strings.

Assuming you loaded your data with URLLoader and specified the dataFormat as URLLoaderDataFormat.VARIABLES, you are close.

If you have a raw string, you should parse it first to break it down to name/value pairs. This is what URLVariables does.

Anyway, once you have an object containing names/values, you can do:

var i:int;
for (i = 0; i < 355; i++)
{
    var tempString:String = "vote" + i; 
    voteResults[i] = event.target.data[tempString];
}

If you use dot access, it will take "tempString" literally. If you use bracket access, the value of the variable tempString will be evaluated.

PS: By the way, I don't think your php will do what you want. A cleaner way, IMO would be:

$pairs = array();
for ($i = 0; $i < 355; $i++)
{
    $pairs[] = 'vote' . $i . '=' . $votesArray[$i]; 
    // you might want to use rawurlencode($votesArray[$i]) to be safe if these are not numeric values.
}
echo implode('&',$pairs);

PS 2: Also, this is rather brittle since you're hardcoding 355. If you ever change your php, you'll have to change your AS code as well. You could try something like this:

var data:URLVariables = event.target.data as URLVariables;
for(var fieldName:String in data) {
    var index:int = parseInt(fieldName.substr(4));
    //  or optionally, add an underscore to the name: vote_1
    //  so you can change the above to something like
    //  fieldName.split("_")[1]
    voteResults[index] = data[fieldName];
}

Or, as I said before, you could use xml, which is simple enough and a better option for this, I think.

Thursday, October 20, 2022
 
5

Multiple inheritance is not possible in AS. But with interfaces you can mimic some of the functionality of multiple inheritance. MI has major flaws, most notably the diamond problem: http://en.wikipedia.org/wiki/Diamond_problem That's why many languages don't support MI, but only single inheritance. Using interfaces it "appears" you apply MI, but in reality that is not the case since interfaces don't provide an implementation, but only a promise of functionality.

interface BadAss{
    function doSomethingBadAss():void;
}

interface Preacher{
    function quoteBible():void;
}

class CrazyGangsta implements BadAss, Preacher{
    function quoteBible():void{
        trace( "The path of the righteous man is beset on all sides by the inequities of the selfish and the tyranny of evil men." );
    }
    function doSomethingBadAss():void{
        //do something badass
    }
}

var julesWinnfield : CrazyGangsta = new CrazyGangsta();
julesWinnfield.doSomethingBadAss();
julesWinnfield.quoteBible();

//however, it mimics MI, since you can do:

var mofo : BadAss = julesWinnfield;
mofo.doSomethingBadAss();
//but not mofo.quoteBible();

var holyMan : Preacher = julesWinnfield;
holyMan.quoteBible();
//but not holyMan.doSomethingBadAss();

P.S.: In case you'd wonder: There's no diamond problem with interfaces since an implementor of the interfaces must provide exactly one implementation of each member defined in the interfaces. So, even if both interfaces would define the same member (with identical signature of course) there will still be only one implementation.

Monday, December 19, 2022
 
sanbez
 
3

Someone who has been coding with Flex daily for 2-3 years should have a pretty deep knowledge of the framework. They shouldn't just know how to use the framework but also how the framework itself works and how to extend it. If they don't, you probably don't want to hire them. :)

Advanced Flex developers should understand how UIComponent works and be able to explain the purpose of all these methods:

initialize
stylesInitialized
createChildren
invalidateProperties / commitProperties
invalidateSize / measure
invaldiateDisplayList / updateDisplayList

setActualSize
getExplicitOrMeasuredWidth/Height
validateNow
getStyle / setStyle / clearStyle

They should know what the Flex "invalidation model" is and how it affects the "invalidate" methods and their counterparts. They should also be able to discuss a few of these topics:

  • How does container layout work? How does a Box container decide how to position and size its children?
  • How do Lists display their data and what makes item renderers special? How is a List different from a Repeater?

It's impossible to cover all of the things that a Flex dev should know in a short post here but having a deep understanding of UIComponent, its lifecycle and the invalidation model is super important.

Tuesday, December 27, 2022
 
3

To extend Amarghosh's answer, look to the constructor of ImageSnapshot

ImageSnapshot(width:int, height:int, data:ByteArray, contentType:String)

The data field isn't expecting BitmapData pixel data (bmp.getPixels), it's expecting data encoded in the given contentType. So you could do:

var encoder:PNGEncoder = new PNGEncoder();
var bytes:ByteArray = encoder.encode(bmp);
new ImageSnapshot(width, height, bytes, encoder.contentType);

Once you have to encode it yourself anyway, you should probably do away with the second ImageSnapshot reference and use:

new FileReference().save(bytes, "abc.png");
Wednesday, September 28, 2022
 
esorton
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :