I apologize if this is too vague, But I want to search each JSON object by a unique value and then spit out another value. I have been told it would be a good idea to create a Hash Table for this issue, but I do not know how to go about this. For example:
{
"form_key" : "basicPatientName",
"data" : "Miranda Jones",
"cid" : 2,
"pid" : 1,
"no" : "0"
}
I want to search basicPatientName and be able to pull Miranda Jones, or search basicPatientgender and pull 1.
I used a library called DefiantJS which lets me easily loop through my JSON and lets me do exactly what I am asking, but I have been told that there are too many iterations involved if I want to do this 1000+ times in the same program.
You could use this:
data = [{
"form_key" : "basicPatientName",
"data" : "Miranda Jones",
"cid" : 2,
"pid" : 1,
"no" : "0"
},
{
"form_key" : "basicPatientGender",
"data" : "1",
"cid" : 4,
"pid" : 1,
"no" : "0"
}
];
var result = {};
data.forEach(function (rec) {
result[rec.form_key] = rec.data;
});
// output result
console.log(result);
// Example look-up use:
var data = result.basicPatientName; // = Miranda Jones
Here is the server side code I am trying to send json
$x = array();
$timestamp = strtotime('22-09-2008');
$x["x"] = $timestamp;
$x["y"] = 22;
$val = '[{ "name": "weight", "dataPoints": ['.json_encode($x).'] }]';
echo json_encode($val);
So output for above code looks like
"[{ \"name\": \"weight\", \"dataPoints\": [{\"x\":1222041600,\"y\":22}] }]"
Below is the client side code I get the data via Jquery getJSON
var jqxhr = $.getJSON( "https://domain/gettracker.php?id="+id, function(data) {
console.log(data);
})
I suppose getJson converts json to object automatically , but it logs the raw json like below
"[ { name: "weight", dataPoints: [{"x":1222041600,"y":22}] } ]"
I tried to do json parse , but i get error.
I guess I am not sending the data properly via php.Can some one guide me ?
Your JSON string is not valid - the property names should be enclosed in double quotes - ", and you don't need to encode the string again.
$val = '[{ "name": "weight", "dataPoints": ['.json_encode($x).'] }]';
echo $val;
Or better yet, use json_encode to create the string for you:
$data = array(
'name' => 'weight',
'dataPoints' => $x
);
echo json_encode($data);
In my php code, I am retrieving json data as below.
<?php
$url = "http://dbpedia.org/data/Los_Angeles.json";
$data = file_get_contents($url);
echo $data;
?>
The javascript code consumes this json data returned from php and gets the json object as below.
var doc = eval('(' + request.responseText + ')');
How to retrieve the following json data using dot notations where the keys contain URLs.
My guess is to use encoding. Please guide along these lines
"http://dbpedia.org/ontology/populationTotal" : [
{ "type" : "literal",
"value" : 3792621 ,
"datatype" : "http://www.w3.org/2001/XMLSchema#integer" } ] ,
"http://dbpedia.org/ontology/PopulatedPlace/areaTotal" : [
{ "type" : "literal",
"value" : "1301.9688931491348" ,
"datatype" : "http://dbpedia.org/datatype/squareKilometre" }
I have the following json object and I would like to read it (access some data from it)
how can I do it in Javascript please ?
var allSites =
{"oldSites":
[
{
"site0" : "http://site0.com/",
"site1" : "http://site1.com/",
"site2" : "http://site2.com/"
}
],
"newSites":
[
{
"site0" : "http://site0/new",
"site1" : "http://site1/new",
"site2" : "http://site2/new"
}
]
};
This is what I did but I get undefined.
var allSites = eval(allSites);
alert(allSites.oldSites.site0);
Thanks.
If your object is defined like you have described, it is not JSON, you do not need to use eval.
Then, since oldSites is an array, you have to index it, like oldSites[0] to get the first value.
Then, get the site0 key of the object retrieved.
So you should use: allSites.oldSites[0].site0
Use
allSites.oldSites[0].site0
allSites.oldSites is an array. So you have to iterate using index
Please remove the square brackets which make oldSites an array
var allSites =
{"oldSites":
{
"site0" : "http://site0.com/",
"site1" : "http://site1.com/",
"site2" : "http://site2.com/"
}
,
"newSites":
{
"site0" : "http://site0/new",
"site1" : "http://site1/new",
"site2" : "http://site2/new"
}
};
Well, now i am getting "has to be an array", before it had to be a string.
Can someone help me out of this problem? Check comments.
function publishPhoto() {
var tags = []; var x,y=0;
if ( harBilled == 0 ) {
if ( profilSendt==0) {
var c =0;
//Get the online friends from array!
for ( i=0;i<globalTags.length;i++){
if ( c < 49 ){ //max 50 tags!
tags.push({"tag_uid": ""+globalTags[i]+"",
"x" : ""+(Math.floor(Math.random() * 309) + 1)+"",
"y" : ""+(Math.floor(Math.random() * 309) + 1)+""
});
}
c = c+1;
}
var newJson = new String(array2json(tags));
newJson = newJson.toString();
FB.api('me/photos', 'post', {
message: txt2send,
status: 'success',
url: 'http://meedies.com/0bcf1f22_smush_400x400.jpeg',
}, function (response) {
if (!response || response.error) {
harBilled=0;
alert(var_dump(response.error));
} else {
var fi = response.id;
alert("Now this!");
FB.api(fi + '/tags?tags='+tags, 'POST', function(response){
alert(var_dump(response));
});
harBilled=1;
//getPages()
}
})
profilSendt=1;
}
}
I am trying to insert multiple ids to be tagged on a picture. Can something help me though this correctly
Sounds like you're filling the wrong type of data into the tags array.
Try this...
var tags = [
{"tag_uid": 91839404, "x": 250,"y": 350},
{"tag_uid": 91839401, "x": 220,"y": 340}
];
Edit
Just insert the objects itself and not an array with one single object.
tags.push({"tag_uid": 91839404, "x": 250,"y": 350});
This is the Facebook definition:
PHOTO_ID/tags?tags=[{"id":"1234", "X":1, "y":2}, {"id":"12345", "x":1, "y":2}]
I've just tried to do that with json_encode. The result was:
[{"id":"1","x":"1","y":"2"},{"id":"2","x":"1","y":"2"}]
To post your variable tags, use
console.log(tags);
Doesn't work on old IE browsers.
It's still an array. If the api requires it as string, you'll have to encode it. Like I said, json encoding would return exactly the same "visual" result, except - it would be a string, not an array.
[{"tag_uid": 587908880,"x" : 17,"y" : 251},{"tag_uid": 664099777,"x" : 166,"y" : 197},{"tag_uid": 824600788,"x" : 275,"y" : 89},{"tag_uid": 1012286173,"x" : 247,"y" : 225},{"tag_uid": 1027953684,"x" : 81,"y" : 25},{"tag_uid": 1049653245,"x" : 169,"y" : 2},{"tag_uid": 1089472771,"x" : 236,"y" : 125},{"tag_uid": 1157692807,"x" : 75,"y" : 70},{"tag_uid": 1183641328,"x" : 307,"y" : 254},{"tag_uid": 1206853982,"x" : 154,"y" : 254},{"tag_uid": 1279891790,"x" : 54,"y" : 5},{"tag_uid": 1379771663,"x" : 206,"y" : 280},{"tag_uid": 1446366514,"x" : 37,"y" : 168},{"tag_uid": 1599969496,"x" : 26,"y" : 226},{"tag_uid": 1645141964,"x" : 250,"y" : 23},{"tag_uid": 100000830101385,"x" : 5,"y" : 110},{"tag_uid": 100003711738950,"x" : 174,"y" : 294},{"tag_uid": 100003908889022,"x" : 249,"y" : 38}]
Once the string is formated, you may send it this way:
PHOTO_ID/tags?tags=[{"id":"1234"}, {"id":"12345"}]
Here, you're converting your array to json (I'll assume it's converted correctly)
var newJson = new String(array2json(tags));
newJson = newJson.toString();
But here, you're still using the array
FB.api(fi + '/tags?tags='+tags
I think i've found your problem:
The x and y coordinates that you're using, should be as percentage offset from the left(x) and top(y). Allowed values are 0 - 100. As of your posts below, I see that some of the coordinates exceed the 100 limit. You should probably recalculate your coordinates as relative offsets. But for first, try it with some test data, to see if it works.
Also, see documentation.
Try this, a tell me, how it's gone.