0

it seems to be a really easy question, but I am a little bit struggling: I am receiving a JSON String via JavaScript. Now I would like to iterate through the element. The resulting string has this form: {"title":value,"title2":value}

How can I iterate through this JSON string without knowing the key and value? I would like to get this output:

title -> value

title2 -> value2

I tried it this way:

$json = file_get_contents('php://input');
$array = json_decode($json,true);
$response = "Test";
foreach($array as $key=>$val) {
    $response = $response. "$key : $val";
}

echo json_encode($response);

It only returns "Test". If I change it to echo json_encode($array), it returns the mentioned JSON String.

3
  • wait, are you using php or javascript? You mention both in your question. I answered in php, but it is simple to do in js also Commented Aug 11, 2021 at 7:28
  • JavaScript as a sender and php as a receiver. Commented Aug 11, 2021 at 7:43
  • What have you tried to resolve the problem? Where are you stuck? Have you tried to dump $array, just to see what it contains? Commented Aug 11, 2021 at 8:14

2 Answers 2

2

You mention javascript and php in your question, so I'm going to answer for both. Here is JS, two different ways. I believe that foreach is being deemphasized in favor of the (of) construct now, but I don't work primarily in JS:

var json = '{"title": 12, "title2": "text"}';
var data = JSON.parse(json);

Object.keys(data).forEach(function(key) {
  console.log(key + ' -> ' + data[key])
})

for(key of Object.keys(data)) {
  console.log(key + ' -> ' + data[key]);
}

And for PHP:

You can parse the json string into an array using json_decode:

$json = '{"title": 12, "title2": "text"}';
$arr = json_decode($json, true);
foreach($arr as $key=>$val) {
    echo "$key : $val";
}

true parses it into an array instead of a std object.

https://www.php.net/manual/en/function.json-decode.php

Sign up to request clarification or add additional context in comments.

13 Comments

I added the code I used. Unfortanetly it does not enter the loop. Do you have any idea what might be wrong? Might it be an error because of the type? JSON_decode returns a string.
json_decode is bad in one way because it will fail silently. You can call json_last_error() to get the last error and troubleshoot. I would start by checking the value of $json, to make sure that it is valid json. php.net/manual/en/function.json-last-error.php
json_decode returns a string, so it seems to fail.
You can also check the value of array with something like echo "<pre>"; print_r($array); echo "</pre>"; echo "<pre>"; var_dump($json); echo "</pre>"; exit;
What does json_decode return? Does json_last_error show an error? What does $array contain?
|
0

Because of response's format you must decode the decoded format in order to take the object as you want

$json = '{"title": 12, "title2": "text"}';
$encoded=json_encode($json);
$decoded=json_decode($encoded);
$ddecode=json_decode($decoded);
foreach($ddecode as $key=>$val) {
    echo "$key -> $val";
}

Output :

title -> 12 title2 -> text

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.