I not which part I am doing wrong. I couldn't able to fetch this array to display. Can someone please help me with this. I am new to JSON.
Array
(
[0] => [{"id":2,"request_id":2,"message":"wqvewq ewq wq ewq e wqwe qwe ","user_id":1,"created_at":"2014-05-30 16:21:28","updated_at":"2014-05-30 16:21:28"},{"id":3,"request_id":2,"message":"as aS A","user_id":2,"created_at":"2014-05-30 17:18:37","updated_at":"2014-05-30 17:18:37"},{"id":4,"request_id":2,"message":"AS As a","user_id":2,"created_at":"2014-05-30 17:18:43","updated_at":"2014-05-30 17:18:43"}]
[1] => [{"id":1,"request_id":2,"message":"sfsdfds sdfds f ","user_id":2,"created_at":"2014-05-30 17:15:16","updated_at":"2014-05-30 17:15:16"}]
[2] => []
)
The output you have quoted looks like PHP print_r output, and it's certainly not legal JSON.
Perhaps you need the PHP json_encode function, to get real JSON out of your PHP code?
It's not one json string but an array of json strings.
You have to first loop thru the array, parse the json and show the variables that you want in your html with jQuery.
You can find a lot of info on the internet and stackoverflow on this subject.
these are the possibilitys you have
var data = array();
for(var i=0;i<yourArray.length;i++)
data[i] = $.parseJSON(yourArray[i]);
or (untested)
var data = JSON.parse(JSON.stringify({yourArray: yourArray}));
Related
I have a string like this
{"2":{"name":"Moon Center","value":"moon7","value_raw":"moon7","id":2,"type":"select"},"3":{"name":"Multiple Choice","value":"Second Choice","value_raw":"Second Choice","id":3,"type":"radio"}}
How do I get for example, the content inside value to a variable? And I would want to be able to get it for every item with value in there. There will be several that come from a form in a single string.
This is the response from a form that stores the whole string in a string. Kinda wish it was an array but this is what i'm working with.
Your string is actually a JSON value. To get the data out of it, you must first json_decode it to an array (or object). If you choose an array, you can then use array_column to get all the 'value' values:
$json = '{"2":{"name":"Moon Center","value":"moon7","value_raw":"moon7","id":2,"type":"select"},"3":{"name":"Multiple Choice","value":"Second Choice","value_raw":"Second Choice","id":3,"type":"radio"}}';
$array = json_decode($json, true);
print_r(array_column($array, 'value'));
Output:
Array (
[0] => moon7
[1] => Second Choice
)
Demo on 3v4l.org
I've never worked with json before and I'm unsure how to fully. So essentially I need to get all the items in the rgDescriptions table, but I am honestly unsure how to.
So if someone could point me in the right direction that would be great. I've tried to look for documentation/tutorials on Json but can't seem to find advanced stuff.
Only stuff like {"id":"2","instanceID":"8"}
Example of the json I want to decode
use json_desode() to decode the json format.
$json = '{"id":"2","instanceID":"8"}';
$decoded = json_decode($json);
print_r($decoded);// will print decoded format of your json
echo $decoded->id; // outputs => 2
echo $decoded->instanceID; // outputs => 8
Otuput:
stdClass Object
(
[id] => 2
[instanceID] => 8
)
while I was searching through some file in a php library, i found some documents like this
a:1:{i:0;a:1:{s:3:"cnt";s:1:"1";}}
This is definitely not JSON. Does any one know what is this? or is it a custom syntax for the guy who wrote the library ?
That is a serialized data and not any programming language syntax.
For your understanding...
<?php
$arr = ['a'=>1,'b'=>2,'cnt'=>5];
echo serialize($arr);
OUTPUT :
a:3:{s:1:"a";i:1;s:1:"b";i:2;s:3:"cnt";i:5;}
Language in Php more info on this link unserialize
This is basically a serailize form of array
of follwoing array
Array
(
[0] => Array
(
[cnt] => 1
)
)
You can get it back by into array
$a = 'a:1:{i:0;a:1:{s:3:"cnt";s:1:"1";}}';
$unserailize_a = (unserialize($a));
To convert an array into string us the Serialize
I am passing a list of numbers from a page to PHP serialized in JSON
{"items":"[1,2,3,4]"}
in my URL it is just
...&items={"items":[1,2,3,4]}
I decode this in PHP
$json = $_GET["items"];
$arr = json_decode($json, true);
I get an array
Array ( [items] => [1,2,4] )
But when I try a foreach on arr["items"] and print out each value, all I get is a single value
[1,2,4]
This is the code I am using to iterate
foreach($res["items"] as $value)
echo $value;
How come I am not getting something like
1
2
4
Look closely at your json string:
{"items":"[1,2,3,4]"}
Look closer:
"[1,2,3,4]"
You are saying that items is a string containing:
"[1,2,3,4]"
Remove the " and you'll be fine.
Your serialization is wrong. Should be:
{"items":[1,2,3]}
To get rid of problems like that use JSON.stringify in JS:
var myData = {"items" : [1,2,3]},
queryString = 'data='+encodeURIComponent(JSON.stringify(myData));
for IE < 8 it has to be included from external script (see here) :
<!--[if lt IE 8]><script src="/js/json2.js"></script><![endif]-->
Anyway much easier would be to send it already as an array:
items[0]=1&items[1]=2&items[2]=3
This way you can send also more complex structures:
data[items][0]=1&data[items][1]=2
// on PHP side will become
$_GET['data'] = array('items' => array(1,2))
A JSON array has the form:
[[a,b,c],[a,b,c],[a,b,c]]
Is there a better way than split?
No, this is most certainly not the best way to parse JSON. JSON parsers exist for a reason. Use them.
In JavaScript, use JSON.parse:
var input = '[[1,2,3],[1,2,3],[1,2,3]]';
var arrayOfArrays = JSON.parse(input);
In PHP, use json_decode:
$input = '[[1,2,3],[1,2,3],[1,2,3]]';
$arrayOfArrays = json_decode($input);
You do not need to use regular expressions. As has been mentioned, you must first have valid JSON to parse. Then it is a matter of using the tools already available to you.
So, given the valid JSON string [[1,2],[3,4]], we can write the following PHP:
$json = "[[1,2],[3,4]]";
$ar = json_decode($json);
print_r($ar);
Which results in:
Array
(
[0] => Array
(
[0] => 1
[1] => 2
)
[1] => Array
(
[0] => 3
[1] => 4
)
)
If you want to decode it in JavaScript, you have a couple options. First, if your environment is new enough (e.g. this list), then you can use the native JSON.parse function. If not, then you should use a library like json2.js to parse the JSON.
Assuming JSON.parse is available to you:
var inputJSON = "[[1,2],[3,4]]",
parsedJSON = JSON.parse(inputJSON);
alert(parsedJSON[0][0]); // 1
In JavaScript , I think you can use Eval() → eval() method...