473,387 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

JSON inclusion

Currently having a problem getting JSON to include properly. I When I attempt to call .toJSONstring on an array it was giving me an error. So I resorted to just alerting the array out to the screen and here is what prints out:

key is: tmp1, value is: tmp1;
key is: tmp2, value is: tmp2;
key is: tmp3, value is: tmp3;

key is: toJSONstring, value is:
function () {
var a = ['['],
b,
i,
l = this.length,
v;

function p(s) {
if (b) {
a.push(',');
}
a.push(s);
b = true;
}
for (i = 0; i < l; i += 1) {
v = this[i];
switch (typeof v) {
case 'object':
if (v) {
if (typeof v.toJSONString === 'function') {
p(v.toJSONString());
}
} else {
p("null");
}
break;
case 'string':
case 'number':
case 'boolean':
p(v.toJSONString());
}
}
a.push(']');
return a.join('');
};


Now you can see it prints out the appropriate function code for the array prototyped version of the toJSONstring function. Which makes me think I am simply making some simple error in how I am including it.

Here is all of the relevant code below, except for JSON.js which I linked to

PHP index file:
you can see there is the single JS call
------------------------------------------------------------------------------------------------------------------
<?
2 require_once('includes.php');
3
4 $login = new login();
5 $login->user_login('xxxxxx','xxxxxx');
6 ?>
7 <LINK REL=StyleSheet HREF="./css/mylayout.css" TYPE="text/css" MEDIA=screen>
8 <head>
9 <? //<script type="text/javascript" language="javascript" src="js/json.js"></script>?>
10 <script src="js/json.js"></script>
11 <script language="javascript" src="js/mdp.js.php"></script>
12 <script>
13 <?
14 sajax_show_javascript();
15 ?>
16 </script>
17 </head>
18 <div id="main_nav">
19 <?
20 >-------echo '<button onclick="outside_call()">Save</button>';
21
22 ?>
23 </div>
24 <div id="sub_nav">
25 </div>
26 <div id="topToolBar">
27 </div>
28 <div id="basepage">
29 </div>
30 <div id="log">
31 </div>
----------------------------------------------------------------------------------------------------------------

JSON.js: litterally copy and pasted
--------------------------------------------------------------------------------------------------------------
http://json.org/json.js
-------------------------------------------------------------------------------------------------------------

mdp.js
--------------------------------------------------------------------------------------------------------------
1 <?
2 header("Content-Type: application/x-javascript\n");
3 ?>
4
5
6 function outside_call()
7 {
8 var test_array = new Array();
9 test_array['tmp1']='tmp1';
10 test_array['tmp2']='tmp2';
11 test_array['tmp3']='tmp3';
12 alert_array(test_array);
13 //var string;
14 //string = test_array;
15 //alert(string);
16 //alert_array(string);
17 }

41 function alert_array(inArray)
42 {
43 for (var i in inArray)
44 {
45 >-------alert('key is: ' + i + ', value is: ' + inArray[i]);
46 }
47 }

--------------------------------------------------------------------------------------------------------------
Apr 21 '07 #1
1 1630
Figured it out. You have to call it as an object. Since JSON uses the .length op on the array in the array prototype, associative arrays have to be passed as objects. So just change the test_array = Array() to = Object and she works fine:
Apr 23 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: G Matthew J | last post by:
http://htmatters.net/htm/1/2005/07/evaling-JSON.cfm This is more or less in response to Mr Crockford's admonition a few months ago, "fork if you must". Ironically, although in that usenet post...
20
by: Luke Matuszewski | last post by:
Welcome As suggested i looked into JSON project and was amazed but... What about cyclical data structures - anybody was faced it in some project ? Is there any satisactional recomendation... ...
2
by: Kevin Newman | last post by:
Hello, I noticed that the JavaScript library for JSON posted on json.org (http://www.json.org/json.js) is modifying Object.prototype (adding a method - toJSONString). I thought this was...
3
by: Adam | last post by:
I'm trying to retrieve some values from a json object, but instead it's giving me the property name. For example: var json = { "glossary": { "title": "example glossary" } }; console.log(json);...
2
by: kungfumike | last post by:
I am currently having an issue getting json to work. The version of json,js I am using can be found here: http://www.json.org/json.js The file is innclided in standard html <script...
2
by: ChrisO | last post by:
I've been pretty infatuated with JSON for some time now since "discovering" it a while back. (It's been there all along in JavaScript, but it was just never "noticed" or used by most until...
23
by: dhtmlkitchen | last post by:
JSON We all know what it is. In ECMAScript 4, there's a JSON proposal: Object.prototype.toJSONString String.prototype.parseJSON The current proposal, String.prototype.parseJSON, returns...
9
by: Jon Paal [MSMD] | last post by:
using json like ( {"Records": , "RecordCount":"1" } ) and jquery like: $.ajax({ .... success: function(json, status) {
6
by: Lasse Reichstein Nielsen | last post by:
Max <adsl@tiscali.itwrites: Not really. It shows that a particularly naïve implementation of a conversion from XML to JSON doesn't work well. What if the conversion of <e> some
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.