I'm building a super simple website application that calls an API to retrieve backlink data for any website inputted into said application by the user. The data the API sends includes strings (e.g. http://www.domain.com/?feed=rss) and numbers and is in JSON format. I parse the response as follows:
mozResponse = JSON.parse(response);
I then iterate through this data, pushing only the data I want into 2 new arrays (arry, arry1), declared as follows:
arry = [];
arry1 = [];
Pushing as follows:
arry.push({id:i, url:mozResponse[i].uu, pa:Math.round(mozResponse[i].upa), da:Math.round(mozResponse[i].pda), anchor:mozResponse[i].lt});
I then Stringify these two arrays as follows:
var cautionArrayString = JSON.stringify(arry);
var dangerArrayString = JSON.stringify(arry1);
I'm using a JavaScript XMLHTTPRequest to POST this data to a php file as follows:
var queryString = "email=" + Email + "&caution=" + cautionArrayString + "&danger=" + dangerArrayString;
xhr1.onreadystatechange=Response1;
xhr1.open("post","http://example.com/emails.php",true);
xhr1.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xhr1.send(queryString);
The php file then reads:
$caution = $_POST['caution'];
$danger = $_POST['danger'];
I'm aware at this point I should decode the JSON again upon originally doing so I ended up with a broken array. Here's what the console.log reads AFTER posting the data to the php file, with the php file using:
echo($_POST['caution']);
echo ($_POST['danger']);
Console.log shows:
[{"id":3,"url":"example.ca/","pa":26,"da":12,"anchor”:”Example”},
{"id":4,"url":"example.ca/","pa":26,"da":12,"anchor":"thank you gifts"},
{"id":5,"url":"example.ca/","pa":26,"da":12,"anchor":"flowers"},
{"id":6,"url":"example.ca/","pa":26,"da":12,"anchor":"thank you"},
{"id":7,"url":"example.ca/","pa":26,"da":12,"anchor":"Arrive in Style"},
{"id":8,"url":"example.ca/","pa":26,"da":12,"anchor":"dignity"},
{"id":9,"url":"example.ca/","pa":26,"da":12,"anchor":"Beautiful in Blue"},
{"id":10,"url":"example.ca/","pa":26,"da":12,"anchor":"Blooming Garden Basket"},
{"id":11,"url":"example.ca/","pa":26,"da":12,"anchor":"Country Basket Blooms"},
{"id":12,"url":"example.ca/","pa":26,"da":12,"anchor":"Heart’s Delight"},
{"id":13,"url":"example.ca/","pa":26,"da":12,"anchor":"Make a Wish"},
{"id":14,"url":"example.ca/","pa":26,"da":12,"anchor":"Moondance"},
{"id":15,"url":"example.ca/","pa":26,"da":12,"anchor":"Queen’s Court"},
{"id":16,"url":"example.ca/","pa":26,"da":12,"anchor":"Sweet as Sugar"},
{"id":17,"url":"example.ca/","pa":26,"da":12,"anchor":"flower colors"},
{"id":18,"url":"example.ca/","pa":26,"da":12,"anchor":"Always Yours"},
{"id":19,"url":"example.ca/","pa":26,"da":12,"anchor":"Sunrise, Sunset"},
{"id":20,"url":"example.ca/","pa":26,"da":12,"anchor":"Uniquely Chic"},
{"id":21,"url":"example.com/best/index.php?page=1998","pa":25,"da":31,"anchor":"example.ca/"},
{"id":22,"url":"example.com/best/index.php?page=1994","pa":25,"da":31,"anchor":"example.ca/"},
{"id":23,"url":"example.ca/","pa":25,"da":16,"anchor”:”example”},
{"id":28,"url":"example.ca/article/156-best-cms-for-small-business","pa":22,"da":39,"anchor":"example.ca/"},
{"id":30,"url":"example.ca/blog.html","pa":21,"da":15,"anchor":"example.ca/"},
{"id":31,"url":"example.ca/beyond-the-flowers-choosing-a-vase/","pa":20,"da":12,"anchor”:”Example”},
{"id":32,"url":"example.ca/beyond-the-flowers-choosing-a-vase/","pa":20,"da":12,"anchor":"Arrive in Style"},
{"id":33,"url":"example.ca/beyond-the-flowers-choosing-a-vase/","pa":20,"da":12,"anchor":"Beautiful in Blue"},
{"id":34,"url":"example.ca/beyond-the-flowers-choosing-a-vase/","pa":20,"da":12,"anchor":"Blooming Garden Basket"},
{"id":35,"url":"example.ca/beyond-the-flowers-choosing-a-vase/","pa":20,"da":12,"anchor":"Country Basket Blooms"},
{"id":36,"url":"example.ca/beyond-the-flowers-choosing-a-vase/","pa":20,"da":12,"anchor":"Heart’s Delight"},
{"id":37,"url":"example.ca/beyond-the-flowers-choosing-a-vase/","pa":20,"da":12,"anchor":"Make a Wish"},
{"id":38,"url":"example.ca/gifts.html","pa":19,"da":11,"anchor”:”example- <span class=\"catlist\"> Flowers</span>"},
{"id":39,"url":"example.ca/category/flowers/","pa":19,"da":16,"anchor”:”Example”},
{"id":40,"url":"example.ca/category/floral-fauna/","pa":19,"da":16,"anchor”:”Example”},
{"id":41,"url":"nunavut.findstuffhere.ca/nunavut/?feed=rss2[]
Where you can see AT THE END that the 1st array is incomplete and the 2nd is empty (as it should be!). So my question here is, what's causing this and how can I fix it?
Things to Note
I use 3 URL's as inputs to test. The data is complete with 1 URL, but 2 others (the example above included) return this incomplete JSON, seemingly because of a query string being a part of a URL returned from the API?
I console.log(mozResponse) and the output is as expected
I console.log(arry) and console.log(arry1) AFTER iterating through mozResponse and pushing data from mozResponse to arry/arry1 and the output is a complete array
I console.log(arry) and console.log(arry1) AFTER applying JSON.stringify but BEFORE posting to php and the output is complete
Things I've explored
I originally thought this was the result of one of the URL's that the
API returns including a query string (e.g. ?feed=rss2 where it
breaks) however prior to this break point there are query strings
being handled fine
Doesn't seem to be a JSON error as I used
json_last_error(); and it returned 0.
Also doesn't seem to be a a
JSON/POST char limit thing because it returns broken JSON when I
input a different URL (the output from the $_POST for this URL also
breaks following a query string, not sure if this is coincidence)
Suhosin isn't present
Created a phpinfo page to check max_vars but the limit is large
Any help is greatly appreciated!
*different quotes are from copy/pasting!
You probably just need to encode your values for use in a query string:
var cautionArrayString = encodeURIComponent(JSON.stringify(arry));
var dangerArrayString = encodeURIComponent(JSON.stringify(arry1));
var queryString = "email=" + encodeURIComponent(Email) + "&caution=" + cautionArrayString + "&danger=" + dangerArrayString;
Converting it to json does not automatically encode it correctly for use in a url so characters in your values could break the query string.
Assuming the different quotes are caused by copy-pasting...
Related
I have had a read through of some of the questions but they are either written for a different language or written to be solved in a webpage,
Im using node-red to recieve BLE data but the data comes in 1 long string im recieving the following:
string
[{"timestamp":"2020-02-
24T13:44:57Z","type":"Gateway","mac":"AC233FC02D95","gatewayFree":96,"gatewayLoad":0.08}
{"timestamp":"2020-02-24T13:44:59Z","type":"iBeacon","mac":"DB1265E96B6F","bleName":"","ibeaconUuid":"ACFD065EC3C011E39BBE1A514 932AC01","ibeaconMajor":1,"ibeaconMinor":2,"rssi":-51,"ibeaconTxPower":-54,"battery":0}
{"timestamp":"2020-02-24T13:44:59Z","type":"iBeacon","mac":"F0728DEB0B9B","bleName":"","ibeaconUuid":"ACFD065EC3C011E39BBE1A514932AC01","ibeaconMajor":5,"ibeaconMinor":1,"rssi":-30,"ibeaconTxPower":-62,"battery":0}]
my goal is to input the data into node-red in the following format
Gateway-Mac,IbeaconMinor,mac,rssi
i did previously use i different collector to gather data and it gave me the string in the following format
$GPRP,AC233FC02D95,FE2279DCB92B,-70,0201061AFF4C000215ACFD065EC3C011E39BBE1A514932AC0100050003C2
which inputted collected BLE device DATA seperatly rather than in one long JSON string so it was easier to convert and input.
so my question is how can i either change it from
string
[{"timestamp":"2020-02-
24T13:44:57Z","type":"Gateway","mac":"AC233FC02D95","gatewayFree":96,"gatewayLoad":0.08}
{"timestamp":"2020-02-24T13:44:59Z","type":"iBeacon","mac":"DB1265E96B6F","bleName":"","ibeaconUuid":"ACFD065EC3C011E39BBE1A514 932AC01","ibeaconMajor":1,"ibeaconMinor":2,"rssi":-51,"ibeaconTxPower":-54,"battery":0}
{"timestamp":"2020-02-24T13:44:59Z","type":"iBeacon","mac":"F0728DEB0B9B","bleName":"","ibeaconUuid":"ACFD065EC3C011E39BBE1A514932AC01","ibeaconMajor":5,"ibeaconMinor":1,"rssi":-30,"ibeaconTxPower":-62,"battery":0}]
to
string
[{"timestamp":"2020-02-
24T13:44:57Z","type":"Gateway","mac":"AC233FC02D95","gatewayFree":96,"gatewayLoad":0.08}
{2020-02-24T13:44:59Z,DB1265E96B6F,ACFD065EC3C011E39BBE1A514932AC01",1,2,-51,-54,0}
{2020-02-24T13:44:59Z,F0728DEB0B9B,ACFD065EC3C011E39BBE1A514932AC01,5,1,-30,-62,0}]
or how can i input it into mysql in the following format
as mysql string
AC233FC02D95,F0728DEB0B9B51,1,-30
and
AC233FC02D95,DB1265E96B6F,2,-51
before changing hardware i inputted the string like so but with the new json format im not sure what to change in the code
GPRP,AC233FC02D95,FE2279DCB92B,-70,0201061AFF4C000215ACFD065EC3C011E39BBE1A514932AC0100050003C2
var raw = msg.payload;
msg.length = raw.length;
msg.raw = raw;
var data = {};
data.major = raw.slice(-10,-4);
data.mac = raw.substring(5,17);
data.hostname = raw.substring(18,30);
data.minor = raw.slice(-8,-4);
data.rssi = raw.substring(31,34)
var str = data.hostname;
var location = str.replace(`AC233FC02D95`, `PURPLE`)
var beacon = parseInt(data.minor ,16);
var msg = {
topic : "INSERT INTO `test`.`track` (`location`,`beacon`, `mac`,`rssi`)
VALUES ('"+location+"','"+beacon+"', '"+data.mac+"','"+data.rssi+"');"
}
return msg;
Just pass the msg with the string payload through the JSON node, it will parse it to an JSON object, you can then access the fields as normal.
Though from what you've posted the array entries look to be missing , separators.
From there you can build up what ever strings you want or insert the fields directly into the database.
I've been trying to load certain Json with Ajax GET request and then parsing it.
However when trying to access the Json key from HTML script tag it was undefined.
In order to debug this issue, I logged all the keys of Json in console as well as the Json itself. Therefore i utilized this function:
function getInv() {
$.get( "/inventory/", function( data ) {
var invList = data.split(",, "); // Explanation is below
console.log(invList[0]) // Just testing with first object
console.log(Object.keys(invList[0]));
});
}
getInv();
Purpose of data.split(",, "):
Since my backend script uses different programming language, I had to interpret it to the one suitable for Javascript.
There also were multiple Json objects, So i separated them with ",, " and then split them in Javascript in order to create a list of Json objects.
After calling the function, Following output was present:
Although the interesting part is that after pasting Json object in console like this:
This was the output:
So basically, in script tag, i was unable to access object's keys, although once i used it manually in console, all keys could be accessed.
What could be the purpose behind this? It seems quite strange that different outputs are given. Perhaps invList[0] is not Json object at all in the script tag? Thanks!
data.split() returns an array of strings, not objects. You need to use JSON.parse() to parse the JSON string to the corresponding objects.
function getInv() {
$.get( "/inventory/", function( data ) {
var invList = data.split(",, ");
console.log(invList[0]) // Just testing with first object
var obj = JSON.parse(invList[0]);
console.log(Object.keys(obj));
});
}
You can use .map() to parse all of them, then you'll get an array of objects like you were expecting:
var invList = data.split(",, ").map(JSON.parse);
I have a json response from an API and I can't create an array from it with "json_decode" in PHP to iterate through this as an array. I always get "NULL", when I use "var_dump" to print out, what my "json_decode" returns. The response-header of this API response is "application/json", but I am not familiar with this json format.
The json response from the API looks like this:
[1,"Example name","307","7","Test","455",1458572100000]
[1,"Another example name","146","7","Test","455",1458571500000]
[1,"Test","304","7","Test","455",1458572280000]
[1,"Example name 3","163","7","Hello world","455",1458571080000]
This is the result/response of a single API request. Now, for example, I want to get the penultimate number (in this case everytime 455) of every line/object or, for example, the name (second value: "Example name", "Another example name", "Test" and so on). How can I do this with php and this json format? It would be nice, if I can get an array from this to iterate through.
It's a local realtime bus arrival API, but it's similar to/the same as content.tfl.gov.uk/…and I call this API with a simple http/get request using file_get_contents in PHP
"/interfaces/ura/instant_V1?returnList=stopID,stopPointName,LineID,DestinationText,estimatedTime,vehicleID&vehicleID=455")
To clarify, that's not JSON format that you're getting back. But if that's what you're getting then you need a solution :)
I would ignore the first & last character and use str_getcsv() to return the comma separated string as an array:
$input = '[1,"Example name","307","7","Test","455",1458572100000]';
$array = str_getcsv(substr($input, 1, -1));
If you are having multple lines, then you'll want to split them into individual lines before doing the above with:
$lines = explode($input, "]");
$array = array();
foreach($lines AS $line) {
$input = '[1,"Example name","307","7","Test","455",1458572100000]';
$array[] = str_getcsv(substr($input, 1, -1));
}
Updated to show delimeter of ] instead of \n as per comment below.
I am passing an array from php to javascript but it seems to be picking up a extra " at the start and finish of the array.
My array being sent from the PHP file
json_encode($CheckItems."|".$CheckUserItems."|".$CheckUserMessages."|".$CheckCommentsForproducts."|".$CheckComments);
My File reciving the array.
url: 'CheckServer.php',
success: function(data) {
var DataBaseCheck = (data)
DataBaseCheck = data.split("|");
console.info(DataBaseCheck);
Console.info prints ""0","1","2","3""
When checking if Database[0] matches with anther variable it fails due to the extra " when i console log each array i get "0,1,2,3"
How can i solve this i have tried
DataBaseCheck.replace('""','"')
DataBaseCheck.replace('"','')
Array1 = parseInt(DataBaseCheck[0])
I cannot think of any other way to remove them ??
The "extra double quotes" are there because you have a JSON encoded string. You are getting JSON from your PHP code, so in your JavaScript the first step should be to decode it:
function(data) {
data = JSON.parse(data);
data = data.split("|");
# data is now an array of strings, e.g. ["0", "1", "2", "3"]
}
Really though, if you are trying to pass an array of integers from your PHP code to your JavaScript code, rather than using your own delimiter you are better off just creating an array in PHP and JSON encoding that, then your JavaScript would just be JSON decoding and you would have the correct data.
I have a servlet where I send JSON response (back to javascript) . For example , my response looks like
{
"responseStr":"1,5,119.8406677,7,7,116.5664291,10,10,116.6099319,20,10,117.2185898,25,3,115.2636185"
}
Now what is happening at the moment is that I am collecting data( numbers above) in servlet and sending it in JSON response as a String with comma separated values. When this response reaches front end, all these numbers have to go in a javascript array (where I do my further logic). Currently I am doing this by
var res = JSON.parse(REQ.responseText);
var myArr = res.responseStr.split(',');
My thinking is that the second line( where I use split()) is causing a bottleneck in my application . A few data points as in above example are not a trouble but it becomes a problem when i have thousands of data points.
So my question is that is there a way that when I am creating my response in servlet that I can create the response as javascript array so that I do not have to use split() at all?
Any better ways of achieving the above task of converting the response into javascript array?
If you send responseStr as an Array, when the JSON parses it, it will be an array. So you could send your JSON response as "[1,2,3,4,5,6,7]" and so one, and when you JSON.parse, it will return an array.
To make it a little more clear :
var arr = [1,2,3,4,5];
arr = JSON.stringify(arr); // "[1,2,3,4,5]" -- String
arr = JSON.parse(arr); // [1,2,3,4,5] -- Array
In your response set content-type JSON/application and send JSON array
{
"responseStr":["1","5","119.8406677","7","7","116.5664291","10","10","116.6099319","20","10","117.2185898","25","3","115.2636185"]
}
Then in your JavaScript you can simply use (reference):
var myArray = responseJSONObject.responseStr;
You may utilize JSON.js for various tasks.
That is a great question. JSON can return an array as simply as
{ "responseStr": [[1], [2], [3], [4] }
Cool!
Double Quotes are not necessary unless you want them as strings.
One more thing, you can have multi dimensional arrays too!
{ "responseStr": [[1,10], [2,20], [3,30], [4,40]] }
This link is a great reference:
http://json.org/fatfree.html