php - Converting array to JSON string when array already contains JSON strings -


i have array contains json string.

array (     [0] => array         (             [name] => original             [nutrients] => {"calories":{"value":2500,"operator":2},"protein":{"value":500,"operator":1},"carbs":{"value":200,"operator":0},"fat":{"value":50,"operator":0},"sugar":{"value":1,"operator":2}}         )      [1] => array         (             [name] => rest             [nutrients] => {"calories":{"value":5000,"operator":2},"sugar":{"value":10,"operator":2}}         )  ) 

i want turn whole array json string

echo json_encode($array); 

but throws \ in front of quotes

[{"name":"original","nutrients":"{\"calories\":{\"value\":2500,\"operator\":2},\"protein\":{\"value\":500,\"operator\":1},\"carbs\":{\"value\":200,\"operator\":0},\"fat\":{\"value\":50,\"operator\":0},\"sugar\":{\"value\":1,\"operator\":2}}"},{"name":"rest","nutrients":"{\"calories\":{\"value\":5000,\"operator\":2},\"sugar\":{\"value\":10,\"operator\":2}}"}] 

this problem comes because nutrients value json string.

how can convert array json string when contains json strings, while having no slashes in front of quotes?

use json_decode convert 'nutrients' array.

foreach($array &$a){  $a['nutrients'] = json_decode($a['nutrients']);  } 

then

echo json_encode($array); 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -