如何在scala中解析嵌套的json数组

时间:2016-02-11 06:58:29

标签: json scala parsing

我是这样的json。

certificates: [{type: "abc",file: {name: "xyz",path:"/usr/local",extension: "csv"}} ,  {type: "xyz",file: {name: "xyz",path: "/usr/local",extension: "csv"}} , {type: "nmo",file: {name: "xyz",path: "/usr/local",extension: "csv"}}]

此解决方案在我的情况下不起作用。

var list = (jsonValue \ "certificates").as[List[Map[String,String]]]

有人可以建议如何解析这个吗?

1 个答案:

答案 0 :(得分:3)

使用Play JSON:

  val json =
    """{ "certificates": [{"type": "abc","file": {"name": "xyz","path":"/usr/local","extension": "csv"}} ,  {"type": "xyz","file": {"name": "xyz","path": "/usr/local","extension": "csv"}} , {"type": "nmo","file": {"name": "xyz","path": "/usr/local","extension": "csv"}}] }"""

  val jsonValue = Json.parse(json)

  val list = (jsonValue \ "certificates").as[List[Certificate]]

你可以这样使用:

// Source image
$image = imagecreatefrompng("test.png");
$w=imagesx($image);
$h=imagesy($image);
$finalImage = imagecreatetruecolor($w, $h);

// Bg add
$backgroundColor = imagecolorallocate($finalImage, 252,252,252); // gray
imagefill($finalImage, 0, 0, $backgroundColor);

// New sizes calculation
$percent = 0.5;
$new_width = $w * $percent;
$new_height = $h * $percent;

// First image add
imagecopyresampled($finalImage, $image, $new_width, $new_height, 0, 0,
                   $new_width, $new_height, $w, $h);

// Second image rotate with transparant bg
$transparency = imagecolorallocatealpha( $image,0,0,0,127 );
$rotatedImage = imagerotate( $image, -60, $transparency, 1);
imagealphablending( $rotatedImage, false );
imagesavealpha( $rotatedImage, true );

// Getting new rotated image sizes to avoid cutting border
$rw = imagesx($rotatedImage);
$rh = imagesy($rotatedImage);

// Second rotated image add with bigger space
imagecopyresampled($finalImage, $rotatedImage, 0, 0, 0, 0, 
                   $rw*$percent, $rh*$percent, $rw, $rh);
// Outputing png image 
header( 'Content-Type: image/png' );
imagepng( $finalImage );