PHP - How can I iterate through this json response? - javascript

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,DestinationT‌​ext,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.

Related

How to convert a string to JSON format?

I was getting list in array form. So at first place i converted array list to string-
var myJsonString = JSON.stringify(result);
myJsonString="[{"productId":"PI_NAME",
"firstName":null,
"lastName":null,
"customer":null
},
{"productId":"PI_NAME",
"firstName":null,
"lastName":null,
"customer":null
}]"
But again i need to convert myJsonString to Json format, What i need to do? I mean i need to replace 1st" and last ", I guess
You need to call parse now.
JSON.parse(myJsonString)
First, if you ever find yourself building a JSON string by concatenating strings, know that this is probably the wrong approach.
I don't really understand how the first line of your code relates to the second, in that you are not doing anything with JSON-encoded string output from result, but instead just overwriting this on the following line.
So, I am going to limit my answer to show how you could better form JSON from an object/array definition like you have. That might look like this:
// build data structure first
// in this example we are using javascript array and object literal notation.
var objArray = [
{
"productId":"PI_NAME",
"firstName":null,
"lastName":null,
"customer":null
},{
"productId":"PI_NAME",
"firstName":null,
"lastName":null,
"customer":null
}
];
// now that your data structure is built, encoded it to JSON
var JsonString = JSON.stringify(objArray);
Now if you want to work with JSON-encoded data, You just do the opposite:
var newObjArray = JSON.parse(JsonString);
These are really the only two commands you should ever use in javascript when encoding/decoding JSON. You should not try to manually build or modify JSON strings, unless you have a very specific reason to do so.

JSON data being received in an incomplete format in php?

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...

Properly format JSON array in Javascript

I'm writing a simple delete function. This function deletes entries in a list that is being outputted by PHP. This is what happens:
The javascript selects the checked boxes in this list and gets its ID 1,2,3etc.
The created array is converted to JSON using JSON.stringify
PHP gets the POST and decodes it using json_decode() (this fails)
$(document).ready(function(){
$('.action_trash').on('click', function(){
//Select items to trash
var TrashItems = [];
$('input:checked').each(function() {
TrashItems.push($(this).attr("id"));
});
//Convert to JSON
var TrashJSON = JSON.stringify(TrashItems);
alert (TrashJSON);
$.ajax({
type: "POST",
url: "pages/forms_intake/actions/trash/model_trash.php",
data: {TrashItems:TrashItems},
cache: false,
success: function(){
// do stuff to remove trashed elements from list.
}
});
});
});
I'm trying to convert the simple Javascript array to JSON. But I think the JSON is invalid. When I alert the TrashJSON variable, this is the output: ["31","32"]
<?php
session_start();
include "../../../../php/config.php";
$TrashJSON = $_POST['TrashItems'];
var_dump(json_decode($TrashJSON));
//Trash items
//$TrashQuery = mysqli_query ($mysqli, "UPDATE forms_intake SET item_trashed=1 WHERE formid='$TrashInt'");
//Close connection
mysqli_close($mysqli);
?>
When I run this, I'm getting this error:
<br />
<b>Warning</b>: json_decode() expects parameter 1 to be string, array given in <b>/home/nickvmf103/domains/a-training.nl/public_html/sotwebapp/pages/forms_intake/actions/trash/model_trash.php</b> on line <b>7</b><br />
NULL
According to the PHP manual on json_decode, null gets outputted if the JSON is invalid.
So my question is: How to correctly format the JSON in my javascript? So that it can be passed to PHP and converted to a PHP array.
Additional info:
When successfully converted to PHP array, I will use implode to put " OR " between the array values, convert it to a string and use it in my SQL query WHERE statement to trash the items that were checked on my page.
In order for the existing PHP to function how you expect, you need to send TrashJSON instead of TrashItems. TrashItems is the array you've populated, while TrashJson is the JSON encoded version of TrashItems.
Change
data: {TrashItems:TrashItems},
to..
data: {TrashItems:TrashJSON},
jQuery is correctly interpreting TrashItems as an array, and thus sending it as an array of values to the PHP script.
Alternatively, as Barmar has inferred in the comments, you could simply loop over the values array currently posted as TrashItems. You could do this using the following code.
foreach ($_POST['TrashItems'] as $TrashItem) {
echo "$TrashItem\n";
}
This would output each item of the TrashItems array you originally passed in on a separate line, and provide the output of..
31
32

Random " Being picked up by javascript

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.

create javascript array in json response in Java

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

Categories

Resources