I'm looking into saving an array to a text file for later access, but i was wonder what way is better to save this array? Its really just an opinion question! I really just want to know what would be the better way to save this as well as why is there a null in there on the first part?
{
"questionbanknames": "Question Bank Name",
"question": [null, {
"question": "This is question 1?",
"type": "manual",
"a": "",
"b": "",
"c": "",
"d": ""
}, {
"question": "This is question 2?",
"type": "multi",
"a": "yes",
"b": "no",
"c": "maybe",
"d": "all of the above"
}]
}
OR
"questionbanknames": "Question Bank Name",
"question[1][question]": "This is question 1?",
"question[1][type]": "manual",
"question[1][a]": "",
"question[1][b]": "",
"question[1][c]": "",
"question[1][d]": "",
"question[2][question]": "This is question 2?",
"question[2][type]": "multi",
"question[2][a]": "yes",
"question[2][b]": "no",
"question[2][c]": "maybe",
"question[2][d]": "all of the above"
I prefer storing JSON format without serializing. It makes it easier for multiple clients to consume it in the future. Also creating a PHP Object from a JSON string is a breeze.
So I would serialize if it is not JSON, I would save as-is if it is JSON.
As you have Json Encoded string I would suggest to store this in Database instead in a file. Encode before saving and get with json decode as array.
Handling JSON from Database much easier than storing in a file and fetching from file. what if you have thousands of questions.
Related
Im not sure if this would be considered proper format of a json structure. Essentially what Im trying to have is an array of associative arrays. My json data:
{
"notifications": [{
"notificationid": "7hstyans",
"notificationtitle": "Some alert title",
"notificationtype": "SPECIAL ALERT",
"dateCreated": "1502203175"
}, {
"notificationid": "9ksh7dh2",
"notificationtitle": "This is a old notification",
"notificationtype": "OLD ALERT",
"dateCreated": "1502138431"
}, {
"notificationid": "iksnudo3",
"notificationtitle": "new notification",
"notificationtype": "SOME ALERT",
"dateCreated": "1501000523"
}]
}
I am looking for a way to access it such as data.notifications[0]["notificationid"]
Would this be considered the correct format for json and the correct way to access it or what would be the best approach to format this?
Just use json_decode.
Example:
$data = json_decode($jsonString, true);
echo $data['notifications'][0]['notificationid'];
I have a JSON output I would like to convert to a PHP array.
I tried with json_decode(), the problem is that there are arrays in the arrays.
They are the first weapons with PHP and I have never used JSON.
Can anyone help me?
Here is the JSON code:
{
"a": "text",
"b": "",
"c": [
{"name": "1", "id": "some text 1", "val": "x"},
{"name": "2", "id": "some text 2", "val": "x"},
{"name": "3", "id": "some text 3", "val": "x"}
]
}
I have to check that a variable is equal to name 1, contained in c, and if so, it also takes its id and val.
How can I do?
PS: I can compare two variables, but I do not know how to find name 1 and the corresponding data ..
$json = '{"a":"text","b":"","c":[{"name":"1","id":"some text 1","val":"x"},{"name":"2","id":"some text 2","val":"x"},{"name":"3","id":"some text 3","val":"x"}]}';
$json = json_decode($json,true);
echo $json["a"]."<br>";
echo $json["b"]."<br>";
echo $json["c"][1]["name"]."<br>";
echo "<pre>".print_r($json,true)."</pre>";
I'm using PHP to fetch JSON stored in a MySQL database, put it into a PHP variable, and pass it to JQuery, which writes the content to the page:
data = $.parseJSON('<?=$my_json;?>');
$.each(data.links, function(entryIndex, entry) {
//do some stuff here
});
Everything works fine when the JSON looks like this:
{
"links": [{
"url": "http://domain1.com",
"title": "Title 1",
"description": "This is an example for my question on Stack Overflow"
}, {
"url": "http://domain2.com",
"title": "Title 2",
"description": "This is another example for my question on Stack Overflow"
}, {
"url": "http://domain3.com",
"title": "Title 3",
"description": "This is a third example for my question on Stack Overflow"
}]
}
But when the JSON content includes quotes, I get an error reading "Uncaught SyntaxError: Unexpected end of JSON input," even though the quotes are escaped, like this:
{
"links": [{
"url": "http://domain1.com",
"title": "Title 1",
"description": "This is an \"example\" for my question on Stack Overflow"
}, {
"url": "http://domain2.com",
"title": "Title 2",
"description": "This is another \"example\" for my question on Stack Overflow"
}, {
"url": "http://domain3.com",
"title": "Title 3",
"description": "This is a third \"example\" for my question on Stack Overflow"
}]
}
What am I doing wrong?
data = $.parseJSON('<?=$my_json;?>');
The thing you're doing here is you're trying to wrap up the JavaScript object around quotes and passing it as a string to $.parseJSON function.
This function only parses JSON string which is a string should be formed with JSON structure and that will then converted to a JavaScript object by the function.
While $my_json variable already has a JavaScript object, you don't need to use $.parseJSON function. You can just assign it to the JavaScript variable like the following one:
data = <?= $my_json ?>;
$.each(data.links, function(entryIndex, entry) {
//do some stuff here
});
I have passed JSON encoded parameters by POST which we have captured and decoded in another PHP file. I have used the following code to do that.
$entityBody = file_get_contents('php://input');
$entityBody = json_decode($entityBody, true);
I have passed the JSON encoded parameters as follows:
{
"id": "5",
"name": "abcd",
"imei": "1234"
}
Here my code works perfectly fine. However, I want to get all the parameters into a single object so that we can store them efficiently because otherwise there will be too many ifs and elses to get each parameter. So I have encoded the parameters as follows:
device = {
"id": "5",
"name": "abcd",
"imei": "1234"
}
But it is not working. Being new to JSON and PHP I do not know how to handle such cases. How can I achieve this?
use json_decode($_POST['device'], true) since your actually passing a parameter called 'device' to the php file.
You should pass json objects as follow:
{"device" : {
"id": "5",
"name": "abcd",
"imei": "1234"
}}
or if you have an array of devices
{"device" : [{
"id": "5",
"name": "abcd",
"imei": "1234"}
]}
I am very new to JSON and i really need help. So I have been given this $oldAuctions object and after using PHP's json_decode function (json_decode($oldAuctions, TRUE);) it returned me something like this that has 11 objects with 9 set of name/value pairs in each...
{
"recent_results": [
{
"closing_yield": "0.800",
"auction_id": "106",
"use_buy_it": "0",
"issuer": "National Bank",
"term": "1 Year",
"is_charity": "1",
"end_date": "12-26-2012",
"closing_price": "100.000000",
"issue_type": "FDICs"
},
{
"closing_yield": "1.090",
"auction_id": "339",
"use_buy_it": "0",
"issuer": "National Bank",
"term": "1 Year",
"is_charity": "1",
"end_date": "12-12-2012",
"closing_price": "100.000000",
"issue_type": "FDICs"
},
{
"closing_yield": "2.000",
"auction_id": "041",
"use_buy_it": "0",
"issuer": "National Bank",
"term": "5 Year",
"is_charity": "1",
"end_date": "09-11-2012",
"closing_price": "100.000000",
"issue_type": "FDICs"
}
]
}
Now I need to grab each pair and save their values in an array. For example, I want to grab the auction_id and save it's values in an array.....how can I do that?
Also, just for simple testing purpose I tried to print out the values first...but that didn't work either...
foreach($oldAuctions as $IDs)
{
echo 'Ids: '.$IDs->auction_id;
}
I would really appreciate your help. Thank you!
After invoking json_encode your array $oldAuctions has become a JSON string. You can not iterate through it as array or object as its a string.
As $oldAuctions is already an array you can simply use forach
foreach($oldAuctions['recent_results'] as $result){
echo 'Ids: '.$result['auction_id']. "\n";
}