I encoded a mysql result from php to json.. I need to decode it in javascript. Say my string returned is
[{"0":"x","1":"z"},{"0":"xs","1":"zz"}]
Please help me on how to get the value of a particular row and column.. for instance how to get the value of "0" of the second row.
EDIT:
Sorry for bothering friends my mistake.. the typeof returned as string JSON.parse(data) did the trick..
var data = [{"0":"x","1":"z"},{"0":"xs","1":"zz"}];
alert (data[1]["0"]);
gives you the xs
The [] represents an array structure, with each {} being an element in the array. Then, within each object there is a set of attributes, which you get it via the attribute's identifier. In this case, it's 0, so it could have just be 0 as well.
Why make objects? It is very poor coding standard to NAME attributes with a number.
Since it obviously are arrays do this instead
var x = [["x","z"],["xs","zz"]]
Then
x[1][0] will give you xs
Related
I have an array object below
33 => 'a:2:{
s :8:"latitude";
s:10:"39.3600586";
s:9:"longitude";
s:18:"-84.30993899999999";
}'
And here is the variable I'm using to get the value of that object
$events_location = $entries[1]['33'];
Is there a way to get just the value of either the latitude or longitude instead of everything in the single quotes?
Thanks!
What you have here is a serialized string. Unserialize it to access the key in the array:
$events_location = unserialize($entries[1]['33']);
echo $events_location['longitude'];
This should be a comment but its a bit long.
The string you have shown us looks vaguely like part of a serialized php entity. But it's not. If you try to unserialize this you'll get an error. The underlying data appears to be a coordinate - but even ignoring the syntactic errors, the semantics of the structure are wrong.
Please check the source you transcribed this from. If you have not copied the original content here please amend your question.
If you have transcibed it correctly then go speak to whoever supplied you with this data and ask them to fix it.
I have problem with a json, this json keeps updating, so the last entry will never be the same. This is the json:
{"channel":{"id":274304,"name":"Water","description":"Record water consumption.","latitude":"0.0","longitude":"0.0","field1":"Water","created_at":"2017-05-17T16:27:46Z","updated_at":"2017-05-19T01:01:07Z","last_entry_id":28},"feeds":[{"created_at":"2017-05-19T00:51:35Z","entry_id":1,"field1":"288"},{"created_at":"2017-05-19T00:51:50Z","entry_id":2,"field1":"304"}]}
I have something like this in my php code, where the variable $cool contains the decoded json.
$x= $cool->feeds[2]->field1;
But when the json updates, feeds[2] will not be the last entry. So i was thinking of using arrays to store all data. But i don't know how to do this. Can you help me?
If $cool->feeds is an array as I assume, you can use end to get its last item:
$x = end($cool->feeds)->field1;
If you want to get the last element, you also can use array_pop()
$x = array_pop($cool->feeds)->field1;
I am receiving some JSON from PHP like this (previewed in Firefox tools):
1:Object
0:Object
1:Object
2:Object
3:Object
Name: "SomeName"
I now want to iterate through the objects, but not the Name key. It seems if I do an $.each it includes the key/value pair. How can I avoid it? Also why can I choose a numerical value for the first Object (I have it "1") but not assign a value to it? I wish it could look like this for example
1:SomeNameIGaveIt
0:Object
1:Object
2:Object
3:Object
That would make my Name k/v pair obsolete.
JSON
{"1": {"Name":"SomeName", "0":{"data":"data"}. "1":{"data":"data"}}}
In JavaScript, a String is a "subclass" of Object. Lucky for you, there's the typeof operator;
in the Chrome console
var a = "foo" //makes a String
var b = {} //makes an empty Object
typeof a
"string"
typeof b
"object"
Using this, you can check to ensure something is not a string before doing an operation on it.
Other option: instead of iterating using the json $.each call, you could just iterate over it via a JavaScript for-loop. If you know the number of elements inside your SomeNameIGaveIt object is fixed, you could use a fixed number of iterations and then use the array indexing operator (SomeNameIGaveIt[index]), or use a for-in loop (for (var i in array){...})
If you use a for-in loop, you can check if the index is a number using typeof as mentioned above. Any of these approaches is going to yield pretty similar results, so pick whatever suits you best.
So im trying to figure out the best way to get MySql table data into either a multidimensional PHP array or convert that multidimensional array into a json string.
Essentially what im trying to do is have a php include that returns the JSON string so i can iterate through it. I am needing a single key with multiple values, so im not 100% sure that im headed in the right direction.
I want to assign multiple values to the same key, for example:
[{"key1": "package1", "package2", "package3"}, {"key2": "package1", "package2", "package3", "package4"}]
I think that is not going to work right? Because i dont have any type of index's?
That is not valid JSON. The structure you are looking for would be something like:
[
{"key1": ["package1", "package2", "package3"]},
{"key2": ["package1", "package2", "package3", "package4"}]
^ An array as the value to the key "key1", "key2", etc..
]
At the PHP side, you would need something like:
For every row fetched from MySQL
$arr[$key] = <new array>
for each package:
append package to $arr[$key]
echo out json_encode($arr)
JS arrays have an implicit array keying, starting at index 0. What you've got is a perfectly valid JS array, the equivalent of having written:
var x = []; // create new empty array
x[0] = {"key1": .... }; // first object
x[1] = {"key2": ....} // second object
Note that the contents of your {} sub-objects is NOT valid.
You should never EVER built a JSON string by hand. It's too unreliable and easy to mess up. It's just easier to use a native data structure (php arrays/objects), then json_encode() them. Ditto on the other end of the process - don't decode the string manually. Convert to a native data structure (e.g. json_decode(), JSON.parse()) and then deal with the native structure directly.
essentially, JSON is a transmission format, not a manipulation format.
My problem is that the database fields have been badly designed by previous people before me. We are accessing the table rows horizontally.
I am working with PHP and jQuery right now and I am passing data back and forth through AJAX. In PHP I am accessing this array like so:
$attendance['date_lesson_' . $lessonCount]
where lessonCount is a number from 1-10 that is incremented i.e. if lessonCount = 1 then above would be $attendance['date_lesson_1']
I am passing the attendance array through json_encode
How do I therefore access this field data_lesson_ 1 up to 10 in jQuery?
I am trying to do:
var lessonCount = 1;
attendance[i].date_lesson_+lessonCount
//do some stuff with attendance.date_lesson_
lessonCount++;
It was easy in PHP cause you concatenate strings with the dot "." but this isn't a string I'm dealing with in jQuery/JavaScript so how on earth would I do this?
Yes, I know. This is awkward. I agree 100%.
You’ll need to use bracket notation:
attendance['date_lesson_' + lessonCount]
Dot notation can only be used with identifier names as property names.
Just a guess, I don't know how that json looks like.
attendance["date_lesson_"+lessonCount]