Я сделал следующий JSON для обработки некоторых значений:
{
"maxpoints": "10",
"text": "Write a function that...",
"func": "add",
"constraints": ["While", "If"],
"testcases":[
{
"output": "100",
"input": ["10", "30", "60"]
},
{
"output": "100",
"input": ["10", "30", "60"]
},
{
"output": "100",
"input": ["10", "30", "60"]
},
{
"output": "100",
"input": ["10", "30", "60"]
},
{
"output": "100",
"input": ["10", "30", "60"]
}
]
}
И я использовал json_encode, чтобы преобразовать его в строку, жестко закодировать в переменной в PHP, а затем получить поля, используя код ниже:
$question = json_decode(" {\n \t\"maxpoints\": \"10\",\n \t\"text\": \"Write a function that...\",\n \t\"func\": \"add\",\n \t\"constraints\": [\"While\", \"If\"],\n \t\"testcases\":[\n \t\t{\n \t\t\"output\": \"100\",\n \t\"input\": [\"10\", \"30\", \"60\"]\n \t\t},\n \t\t{\n \t\t\"output\": \"100\",\n \t\"input\": [\"10\", \"30\", \"60\"]\n \t\t},\n \t\t{\n \t\t\"output\": \"100\",\n \t\"input\": [\"10\", \"30\", \"60\"]\n \t\t},\n \t\t{\n \t\t\"output\": \"100\",\n \t\"input\": [\"10\", \"30\", \"60\"]\n \t\t},\n \t\t{\n \t\t\"output\": \"100\",\n \t\"input\": [\"10\", \"30\", \"60\"]\n \t\t}\n \t]\n}");
$maxpoints = $question["maxpoints"];
$text = $question["text"];
$func = $question["func"];
$constraints = $question["constraints"];
$testcases = $question["testcases"];
Тем не менее, я сузил свою ошибку, чтобы быть в этих 6 строках, но не могу найти то, что я сделал неправильно, независимо от того, как сильно я выгляжу. Я точно знаю, что моя ссылка на мой php-скрипт в порядке, потому что я использовал несколько операторов print, чтобы увидеть, где остановился поток.
Вот код, который я использую для тестирования этого скрипта в моей консоли chrome:
var formData = new FormData();
formData.append('request_type', 'store_question');
fetch("back.php", {method: 'POST', body: formData})
.then((resp) => console.log(resp.text()));
Это ошибка:
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
Вы получаете ошибку, потому что json_decode
по умолчанию преобразует строку в объект, и вы пытаетесь обратиться к ней как к ассоциативному массиву.
Вы установили второй параметр true
сделать его ассоциативным массивом
json_decode ( $json, true )
Попробуй это:
$question = json_decode(" {\n \t\"maxpoints\": \"10\",\n \t\"text\": \"Write a function that...\",\n \t\"func\": \"add\",\n \t\"constraints\": [\"While\", \"If\"],\n \t\"testcases\":[\n \t\t{\n \t\t\"output\": \"100\",\n \t\"input\": [\"10\", \"30\", \"60\"]\n \t\t},\n \t\t{\n \t\t\"output\": \"100\",\n \t\"input\": [\"10\", \"30\", \"60\"]\n \t\t},\n \t\t{\n \t\t\"output\": \"100\",\n \t\"input\": [\"10\", \"30\", \"60\"]\n \t\t},\n \t\t{\n \t\t\"output\": \"100\",\n \t\"input\": [\"10\", \"30\", \"60\"]\n \t\t},\n \t\t{\n \t\t\"output\": \"100\",\n \t\"input\": [\"10\", \"30\", \"60\"]\n \t\t}\n \t]\n}" , true );
$maxpoints = $question["maxpoints"];
$text = $question["text"];
$func = $question["func"];
$constraints = $question["constraints"];
$testcases = $question["testcases"];
Doc: json_decode
Других решений пока нет …