Select to view content in your preferred language

Flex widget load images from JSON works stangely

4371
2
12-22-2014 06:51 PM
WuYang
by
Emerging Contributor

Dear all,

I am trying to display the images in flex widget got from JSON. All works except the displaying of the images in the widget.

I have 8 image in design mode of the widget, the source will be assigned at runtime when get the data from JSON. The images will be assigned one by one from image1.

I use the same set of JSON data to test. The strange thing is every time the images displayed in the widget is different, for example, sometimes it displays all the 8 images, sometimes it displays image2, image4, image7, sometimes it displays image1, image2, image5.

The source code I use:

private var value0:String;//id

private var value1:String;// type

private var value2:String;// image url

var request:URLRequest=new URLRequest();

request.url=getUrl;

request.requestHeaders=[new URLRequestHeader("Content-Type", "application/json")];

request.method=URLRequestMethod.GET;

var loader:URLLoader=new URLLoader();

loader.addEventListener(Event.COMPLETE, receive);

loader.load(request);

protected function receive(event:Event):void

{

var myResults:Array= com.adobe.serialization.json.JSON.decode(event.target.data);

var i:int;

for (i=0;i<myResults.length; i++){

  value0 = myResults.goods_id;

  value1 = myResults.goods_type;

  value2 = myResults.image_url;

  if (!image1.source){

      image1.source = value2;

  }else if (!image2.source){

      image2.source = value2;

  }else if (!image3.source){

      image3.source = value2;

  }else if (!image4.source){

      image4.source = value2;

  }else if (!image5.source){

      image5.source = value2;

  }else if (!image6.source){

      image6.source = value2;

  }else if (!image7.source){

      image7.source = value2;

  }else if (!image8.source){

      image8.source = value2;

  }

}

Anybody has experience on it? Please advise me, thank you.

Tags (4)
0 Kudos
2 Replies
AnthonyGiles
Honored Contributor

Wu,

not tested but does this work:

for (i=1;i<myResults.length; i++){

  value0 = myResults.goods_id;

  value1 = myResults.goods_type;

  value2 = myResults.image_url;

   this["image" + i].source = value2;

}

0 Kudos
WuYang
by
Emerging Contributor

Thank you Anthony for the reply.

I have tried the code above. no error but also no picture displayed.

0 Kudos