Read JSCON file with PHP - java

I'm trying to read a JSON with a php array. The JSON file is using a format that I am not familiar with and I don't know how to write my PHP array so it could read the file.
JSON file in question can be found here:
http://nhlwc.cdnak.neulion.com/fs1/nhl/league/teamroster/ANA/iphone/clubroster.json
The format is giving me hard time because 1) it is starting with a time stamp that my array cannot read and 2) the file is separated between the value position, means that I have a closing statement ' }] ' before the end of the file - seems like they separated category
My PHP array that work with more standard array :
function myFunction(response) {
var arr = JSON.parse(response);
var i;
var out = "<table>";
for(i = 0; i < arr.length; i++) {
out += "<tr><td>" +
arr[i].position +
"</td><td>" +
arr[i].weight +
"</td><td>" +
arr[i].height +
"</td></tr>";
}
out += "</table>"
document.getElementById("id01").innerHTML = out;
}
thank you

It is called JSON, not JSCON. The code you posted is JavaScript, not PHP.
Here is an example of how you could read the JSON you provided with PHP:
<?php
$result = file_get_contents("http://nhlwc.cdnak.neulion.com/fs1/nhl/league/teamroster/ANA/iphone/clubroster.json");
$json = json_decode($result);
foreach ($json->goalie as $player) {
echo $player->name . '</br>';
}
?>
This will print each goalie's name.

it looks like you need JavaScript(not php) to read this json file?
in JavaScript try
arr = val('(' + response + ')')
and in php try
arr = json_decode(response)

Related

Insert Kotlins ${ } expression while splitting String() in order to add dynamic data after the split

Here is an example of a String:
val message = "Customer name is $name saved on $date"
I needed to find every instance of $variable within the string message and replace it with querySnapShot.get(variable) and that has been answered here Previous Answer. querySnapShot here just contains data from within Firestore listener.
Here is the working Kotlin code:
val message = "Customer name is $name saved on $date"
val arr = message.split(" ").toTypedArray()
for (i in 0 until arr.size) {
val s = arr[i]
if (s.contains("$")) {
arr[i] = "+ querySnapshot.get(" + "\"" + s.substring(1) + "\"" + ")"
}
}
Log.d("Mate", java.lang.String.join(" ", *arr))
which prints:
customer name is querySnapShot.get("name") saved on querySnapshot.get("data)
literally as it is.
QUESTION:
How can I add Kotlin's expression ${} correctly while splitting and joining in order for it to treat querySnapshot.get("variable") as an expression that captures and returns dynamic data after joining? And not just a mere String.
Write
arr[i] = querySnapshot.get(s.substring(1))
The solution isn't to try to use Kotlin string templating, it's to stop putting your own code in strings when you want to run it instead!

Join a json String values with new line

I need to return json array each element in new line. but instead it's printing in one line. I tried using '/n' and space but it's not working.
String json = null;
//split cookie with delimiter to store in array
String wl[] = wishList.split("~");
//JSON RETURN VALUE
json = "[\"";
for(int i = 0; i < wl.length; ++i) {
json += wl[i];
//tried but didn't work
//json.split("\n");
}
json += "\"]";
System.out.println(json);
so i tried cancatinating in for loop but it didn't work.
I need out like this
1.abc
2.bcd
3.efg
but i'm getting this output
1.abcbcdefg
What you are trying to do (if I get it right) is to split a String on the ~ and creating a JSON array with it (one separate line).
Did you thought about simply replacing the character ?
String json = "[" + wishlist.replace("~", ",\n") + "]"; //Added a , to separated each elements
But if you want to do it yourself,
String json = "[";
for(int i = 0; i < wl.length; ++i) {
json += wl[i] + ",\n";
}
json += "]";
You need to add the newLine character, not split it again.
Off course, as pointed, this should be done using a Library design to do it but here is a simple correction of you code.
PS : I didn't write the needed \" for redability. But this can be easily added in both solutions.
I tried using '/n' and space but it's not working.
You should use "\n" in your JSON String to add new line and not "/n", it is not the same thing. You should do the same thing for the regex pattern.
3. Line Separator is '\n'
This means '\r\n' is also supported because trailing white space is ignored >when parsing JSON values.
The last character in the file may be a line separator, and it will be
treated the same as if there was no line separator present.
Source : http://jsonlines.org/
Edit
A Json array has comma to separate values of the array and string should be between quotes or double quotes. but doesn't need "\" at the begining and at the end.
You could get this String :
["abc",
"bcd",
"efg"]
with this code :
final String STRING_DOUBLE_QUOTE="\"";
String json = null;
// split cookie with delimiter to store in array
String wl[] = wishList.split("~");
// JSON RETURN VALUE
json = "[";
for (int i = 0; i < wl.length; ++i) {
if (!json.equals("[")) {
json += ",\n";
}
json += STRING_DOUBLE_QUOTE + wl[i] + STRING_DOUBLE_QUOTE;
}
json += "]";
System.out.println(json);
If you want it to be on multiple lines, you're gonna have to save it in multiple elements i.e. an array.
Try this:
String wl[] = wishList.split("~");
for(int i = 0; i < wl.length; ++i){
System.out.println(wl[i]);
}
Unsure if this is what you're asking for, but this is the answer to your written question

regex get string before matched pattern

I'm working on a project where i have to screen scrape a website and get a string. This is a part of the text.
a href = "/dashboard/index/2971"
title="Project1:Project1">Project1
I need to get the "/dashboard/index/2971" part of the Whole Text using regex. Currently i have this:
while(true){
if (buff.readLine()!=null){
String wholeText = buff.readLine();
System.out.println(wholeText.contains("title=Project1"));
htmlCode += buff.readLine() + "\n";
}else{
break;
}
This just identifies the "title=Project1" String. I need to get the "/dashboard/index/2971" part and put it in a string.
<?php
$str = 'a href = "/dashboard/index/2971" title="Project1:Project1">Projeca...';
preg_match_all('#href\s*=\s*"(.*?)"#', $str, $matches, PREG_SET_ORDER);
$foundURLs = array();
foreach ($matches as $match) {
$foundURLs[] = $match[1];
}
var_dump($foundURLs);

java reading numbers, interpreting as octal, want interpreted as string

i am having an issue, where java is reading an array list from a YAML file of numbers, or strings, and it is interpreting the numbers as octal if it has a leading 0, and no 8-9 digit.
is there a way to force java to read the yaml field as a string?
code:
ArrayList recordrarray = (ArrayList) sect.get("recordnum");
if (recordrarray != null) {
recno = join (recordrarray, " ");
}
HAVE ALSO TRIED:
Iterator<String> iter = recordrarray.iterator();
if (iter.hasNext()) recno = " " +String.valueOf(iter.next());
System.out.println(" this recnum:" + recno);
while (iter.hasNext()){
recno += ""+String.valueOf(iter.next()));
System.out.println(" done recnum:" + String.valueOf(iter.next()));
}
the input is such:
061456 changes to 25390
061506 changes to 25414
061559 -> FINE
it took a while to figure out what it was doing, and apparently this is a common issue for java,
ideas?
thanks
edit: using jvyaml
yaml:
22:
country_code: ' '
description: ''
insection: 1
recordnum:
- 061264
type: misc
yaml loading:
import org.jvyaml.YAML;
Map structure = new HashMap();
structure = (Map) YAML.load(new FileReader(structurefn)); // load the structure file
Where are you reading the file? The problem lies in where the file contents are being read. Most likeley the recordarray list contains integers, ie. they have alreadey been parsed. Find the place where the records are being read. Maybe you are doing something like this:
int val = Integer.parseInt(record);
Use this instead:
int val = Integer.parseInt(record, 10);

how to export a 2D table in java

hello i would like to ask you about the 2D tables in java!!my code is this an i would like to make a system out in order t see the registrations in mytable citylink can anyone help me?
int i=0;
while(i<=citylink.length) {
for(Xml polh_is:fetchsite.child("site").child("poleis").children("polh")) { //url
if((polh_is.string("name")=="")||(polh_is.content()==""))//attribute
error += "Error in polh: name is - " + polh_is.string("name") + " with url - " + polh_is.content() + " -.\n";
else
for(int j=0; j<citylink.length; j++) {
citylink[j][0]=HtmlMethods.removeBreaks(polh_is.string("name"));
citylink[j][1]=HtmlMethods.removeBreaks(polh_is.string("with url -"+polh_is.content() +"-.\n"));
i++;
}
}
}
citylink seems to be a single-dimensional array?
You can use another 2D array to store the values.
Something like this?:
StringBuilder citiesBuilder = new StringBuilder();
for (String[] city:citylink) {
citiesBuilder.append(String.format("%s (URL: %s)%n", city[0], city[1]));
}
System.out.println(citiesBuilder.toString());
EDIT
changed 'cities' to 'citylink' - my mistake. But trust me, it works ;) (Hope your Java is 1.5 at minimum)
ahh, and I assume, citylink is of type String[][]. Otherwise, please provide the declaration so I can adapt the code.
I don't understand the purpose of the second for loop: is it not filling the whole array with the last element?
What about:
int i=0;
for(Xml polh_is:fetchsite.child("site").child("poleis").children("polh")) { //url
if((polh_is.string("name")=="")||(polh_is.content()==""))//attribute
error += "Error in polh: name is - " + polh_is.string("name") + " with url - " + polh_is.content() + " -.\n";
else if (i >= citylink.length)
break;
else {
citylink[i][0]=HtmlMethods.removeBreaks(polh_is.string("name"));
citylink[i][1]=HtmlMethods.removeBreaks(polh_is.string("with url -"+polh_is.content() +"-.\n"));
i++;
}

Categories

Resources