Edited at

読み込んだJSONをLabelに改行付きで表示する

More than 5 years have passed since last update.

{

"text": "改行A\\n改行B"
}

<?xml version="1.0" encoding="utf-8"?>

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
<![CDATA[
protected function btnLoad_clickHandler( event:MouseEvent ):void {
var loader:URLLoader = new URLLoader();
loader.addEventListener( Event.COMPLETE, loadComplete );
loader.load( new URLRequest( "./sample.json" ) );
}

private function loadComplete( event:Event ):void {
var loader:URLLoader = event.target as URLLoader;
var obj:Object = JSON.parse( loader.data );
var value:String = obj.text as String;
var reg:RegExp = /\\n/g;
lblText.text = value.replace( reg, "\n" );
}
]]>
</fx:Script>

<fx:Declarations>
<!-- 非ビジュアルエレメント (サービス、値オブジェクトなど) をここに配置 -->
</fx:Declarations>
<s:VGroup>
<s:Button id="btnLoad" click="btnLoad_clickHandler(event)"/>
<s:Label id="lblText"/>
</s:VGroup>
</s:Application>

RegExpを使って正規表現で\nを\nに置換することでLabel内で改行して表示される