Я использую fancytree для отправки данных через ajax.
$("#save").click(function(e){
e.preventDefault();
var tree = $("#tree").fancytree("getTree").rootNode;
var data = tree.toDict(true);
data = JSON.stringify(data);
$.ajax(
{
type: "POST",
data : { "data": data },
contentType: "application/json",
success: function(data) {
$('#alert').empty().append(data);
}
});
});
Данные JSON являются следующим форматом. Мне нужно повторить следующий порядок объектов JSON для манипулирования данными.
{
"expanded":true,
"key":"root_1",
"selected":false,
"title":"root",
"children":
[
{"key":"p_1","selected":false,"title":"Animal Meat"},
{"key":"p_2","selected":false,"title":"Animal Product"},
{"folder":true,"key":"p_3","selected":false,"title":"Beverage","children":
[
{"key":"2","selected":false,"title":"Tea"},
{"key":"3","selected":false,"title":"Coffee"},
{"key":"4","selected":false,"title":"Water"}
]},
{"folder":true,"key":"p_4","selected":true,"title":"Cereal","children":
[
{"key":"6","selected":true,"title":"Porridge"}
]},
{"folder":true,"key":"p_5","selected":false,"title":"Dairy Product","children":
[
{"key":"7","selected":false,"title":"Cheese"},
{"key":"8","selected":false,"title":"Curd"},
{"key":"10","selected":false,"title":"Milk"},
]}
]}
Следующая функция в Laravel обрабатывает этот запрос.
public function storePreference(Request $request)
{
$data = $request->getContent();
$data = urldecode($data);
foreach($data['children'] as $child)
{
$key = $child['key'];
$selected = $child['selected'];
$title = $child['title'];
printf("Key: %s, selected: %s, title: %s\n", $key, $selected ? "true" : "false", $title);
}
}
Мне нужен следующий вывод:
Key: p_1, selected: false, title: Animal Meat
Key: p_2, selected: false, title: Animal Product
Key: p_3, selected: false, title: Beverage
Key: 2, selected: false, title: Tea
Key: 3, selected: false, title: Coffee
Key: 4, selected: false, title: Water
Key: p_4, selected: true, title: Cereal
Key: 6, selected: true, title: Porridge
Key: p_5, selected: false, title: Dairy Product
and so on...
Я использую fancytree для отправки этих данных в Laravel 5.
Любая помощь приветствуется.
$json = <<<JSON
{
"expanded":true,
"key":"root_1",
"selected":false,
"title":"root",
"children":
[
{"key":"p_1","selected":false,"title":"Animal Meat"},
{"key":"p_2","selected":false,"title":"Animal Product"},
{"folder":true,"key":"p_3","selected":false,"title":"Beverage","children":
[
{"key":"2","selected":false,"title":"Tea"},
{"key":"3","selected":false,"title":"Coffee"},
{"key":"4","selected":false,"title":"Water"}
]},
{"folder":true,"key":"p_4","selected":true,"title":"Cereal","children":
[
{"key":"6","selected":true,"title":"Porridge"}
]},
{"folder":true,"key":"p_5","selected":false,"title":"Dairy Product","children":
[
{"key":"7","selected":false,"title":"Cheese"},
{"key":"8","selected":false,"title":"Curd"},
{"key":"10","selected":false,"title":"Milk"}
]}
]}
JSON;
$data = json_decode($json, true);
foreach ($data['children'] as $child) {
$key = $child['key'];
$selected = $child['selected'];
$title = $child['title'];
printf("Key: %s, selected: %s, title: %s\n", $key, $selected ? "true" : "false", $title);
}
Это выведет:
Key: p_1, selected: false, title: Animal Meat
Key: p_2, selected: false, title: Animal Product
Key: p_3, selected: false, title: Beverage
Key: p_4, selected: true, title: Cereal
Key: p_5, selected: false, title: Dairy Product
Обновить: Я изменил JSON в моем примере, потому что JSON из исходного поста был недействительным на момент написания этой статьи (например: вторая до последней строки в оригинальном JSON содержала завершающую запятую).
Метод Ларавела:
public function storePreference(Request $request)
{
$dataString = $request->getContent();
$dataString = urldecode($data);
$dataString = json_decode($dataString, 'data=');
$data = json_decode($dataString, true);
foreach($data['children'] as $child)
{
$key = $child['key'];
$selected = $child['selected'];
$title = $child['title'];
printf("Key: %s, selected: %s, title: %s\n", $key, $selected ? "true" : "false", $title);
}
}
Мне нужно было удалить «data =» из строки $ data и использовать json_decode.
public function storePreference(Request $request)
{
$data = $request->getContent('data');
$data = urldecode($data);
$data = substr($data, 5);
$data = json_decode($data, true);
foreach($data['children'] as $child)
{
$key = $child['key'];
$selected = $child['selected'];
$title = $child['title'];
printf("<p>Key: %s, selected: %s, title: %s\n", $key, $selected ? "true" : "false", $title);
if (isset($child['children']))
{
foreach ($child['children'] as $c)
{
$key = $c['key'];
$selected = $c['selected'];
$title = $c['title'];
printf("<p>Key: %s, selected: %s, title: %s\n", $key, $selected ? "true" : "false", $title);
}
}
}
}
Спасибо @jeromegamez за помощь.