Is there a maximum length for json in Java? - php

I'm getting an output of the classes that students saw through all the university career.
this is an example of the output
{
"HISTORICOP": [
{
"MATERIA": "PROCESOS DEL LENGUAJE ",
"NOTA": "7 ",
"ANIO": "2000",
"PERIODO": "001",
"ENEMENOSUNO": "0 "
},
{
"MATERIA": "RAZONAMIENTO BASICO FG ",
"NOTA": "13 ",
"ANIO": "2000",
"PERIODO": "001",
"ENEMENOSUNO": "0 "
},
{
"MATERIA": "DESARROLLO DE COMPETENCIAS ",
"NOTA": "8 ",
"ANIO": "2000",
"PERIODO": "001",
"ENEMENOSUNO": "n-1 "
}
]
}
these are 3 of the results
but the whole output are 91 results,
when I run it on a emulator the blackberry is not able to read it
but when I try with less results he can read it!
Is there a maximum json length size so it can be read in java?
this is how I retrieve the info! from SAP
try {
$conn = new sapnwrfc($config);
$fds = $conn->function_lookup("ZCM_GET_HORARIO");
$parms = array('CEDULA' => '16814224');
$results = $fds->invoke($parms);
echo "[".json_encode($results)."]";
$conn->close();
} catch (Exception $e) {
echo "Connection Failed 3";
}

There is no absolute limitation in JSON. In Java there is a limit to the length of a String, which is 2^31-1 or over 2 billion characters long. But given the snippet of code you showed us you are not actually using Java (Java doesn't have "$" before variable names).
There may be a limitation in whatever library you are using which is not a fundamental limitation of the data format or the language.
But if you are having problems with just 91 items (not 91 thousand or 91 million) then it is far more likely that you problem is NOT due to a fundamental size limitation. If you post more about what actual errors you saw you might get a more useful response.

Related

php protobuf uint64 is converted to negative number

I have this proto definition
message Product {
uint64 id = 1;
uint64 order_id = 2;
}
then build the response in PHP
(new Product())
->setId(3)
->setOrderId(3875412541)
what I get then is
{
"id": 3,
"order_id": -419554755
}
the order id in this example is 10 characters long. Starting from 11 characters I get
13 INTERNAL: Failed to parse server response
it only works if the order_id is 8 characters long
{
"id": 3,
"order_id": 38754125
}

How to randomize json

I have a randquotes.json file
{
"author": "William Shakespeare",
"quotes": "A brave flea is a flea that can dare to get its breakfast on the lips of a lion."
},
{
"author": "Winston Churchill",
"quotes": "We live with what we get, but we bring life to what we give."
},
{
"author": "Wolfgang von Gothe",
"quotes": "Knowledge is not enough, we must practice it. Intention is not enough, we must do it."
}
Then I want "author" and "quotes" to be generated randomly with php
How?
thanks for help
Assuming you actually have valid a JSON string...
Convert the json to a PHP datatype using json_decode()
find out how large the array is
generate a random number between 0 and array size
Return that array occurance, I did that as a json string again using json_encode()
$json_string = '
[
{
"author": "William Shakespeare",
"quotes": "A brave flea is a flea that can dare to get its breakfast on the lips of a lion."
},
{
"author": "Winston Churchill",
"quotes": "We live with what we get, but we bring life to what we give."
},
{
"author": "Wolfgang von Gothe",
"quotes": "Knowledge is not enough, we must practice it. Intention is not enough, we must do it."
}
]';
$arr = json_decode($json_string);
// How big is the array
$max = count($arr) -1;
$rand_idx = rand(0, $max);
echo json_encode($arr[$rand_idx]);

How to convert HTML data to json, php, mysql?

how to convert HTML data to json, example as below, description content how to convert as json, it is from mysql, php., how to send json responce as plain text, but description comes from the mysql db as it is, but how to send the json responce api to androind.
public function actionTestanalysis()
{
//echo $keyword=$_POST['keyword'];
$query= Yii::app()->db->createCommand("select * from test ORDER BY id DESC")->queryAll();
$arr = array();
if(count($query) > 0) {
foreach($query as $query){
$arr[] = $query;
}
}
# JSON-encode the response
$json_response = json_encode($arr);
// # Return the response
echo $json_response;
//exit;
}
json responce
[
{
"id": "99",
"name": "Max-Gain on or before 25th January 2016 in Max India Limited.",
"description": "
\r\n\tMax India Limited has announced the Record date for Three way De-Merger as 28th January 2016 (Thursday). <\/div>\r\n
\r\n\t <\/div>\r\n
\r\n\tAnyone want to Gain from the three way De-Merger of Max India Group one should buy the shares of Max India Limited on or before – 25th January 2016 (Cum-Date – Monday Tentavily) otherwise to be on safer side you can buy on or before 22nd January 2016(Friday) and get invested in it.<\/div>\r\n
\r\n\t <\/div>\r\n
\r\n\tIf any investor invests for a period Of 12 – 18 Months , this scrip will be a Multifold - Multi Bagger.<\/div>\r\n
\r\n\t <\/div>\r\n
\r\n\tTo View the full report on Max India Limited authored . <\/div>\r\n
\r\n\t <\/div>\r\n
\r\n\tPlease Click The Below Link<\/div>\r\n
\r\n\t
\r\n\t\thttp:\/\/www.test.com\/index.php\/newsOpportunities\/list\/scroll\/no-pain-all-gain-maximum-benefit-in-max-india-ltd<\/a><\/p>\r\n<\/div>\r\n",
"image": "",
"status": "unlock"
},
You have also one error in the cycle in your code.
Try this:
if (count($query) > 0) {
foreach ($query as $queryElement) {
$el = $queryElement;
$el['description'] = trim(preg_replace('/\s+/', ' ', strip_tags($el['description'])));
$arr[] = $el;
}
}
Try before: echo $json_response set header content type to application/json
<?PHP
header('Content-Type: application/json');
echo $json_response;
Use htmlentities() instead of strip_tags(), in order to retain actual content stored in db.

How does this recursive code to find all permutations of a string work?

I am doing Project Euler problems. I am currently working on the circular primes problem
The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.
There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.
How many circular primes are there below one million?
Although checking if something is primes was easy for me, I could not figure out how to get all the permutations of the numbers. After a good bit of searching for tips on an algorithm to do that, I came across a website which gave code for that in Java, which I adapted to PHP below. However, before proceeding with the problem, I would really like to understand what exactly the different bits of the code are doing, especially in the for loop. What I understand of it so far is that in the for loop, it is starting with an empty prefix and then looping through the string and adding a single element from the string to the prefix, until there is only one element left in the original string, at which point, it echoes it out. Am I understanding this correctly? If not, what am I missing?
<?php
getallcombos("","1234");
function getallcombos($prefix,$string){
if(strlen($string)==1){
echo $prefix.$string."<br>";
}
$array=str_split($string);
for($i=0;$i<strlen($string);$i++){
$newstr=substr($string,0,$i).substr($string,$i+1);
getallcombos($prefix.$array[$i],$newstr);
}
}
?>
The problem does not ask for permutations, but rotations. This is different. For all rotations, you can do a loop:
var number = "2031";
var rotations = [];
for (i = 0; i < number.length; ++i) {
number = number.substring(1)
+ number[0];
rotations.push(number);
}
console.log(rotations);
http://jsfiddle.net/T6Mur/
UPDATE
Specially for you:
function allRotArePrime(number) {
var int;
for (i = 0; i < number.length; ++i) {
int = parseInt(
number.substring(i) +
number.substring(0, i)
);
// if (!isPrime(int)) return false;
console.log(int);
}
//return true;
}
var num = 1927;
allRotArePrime(num.toString());
http://jsfiddle.net/T6Mur/3/
The if is your stopping condition. If string is only length 1 you only have one permutation.
the for loop is complex but it does something like this:
"" "1234" -> loop 4 times, giving:
"1", "234"
"2", "134"
"3", "124"
"4", "123"
Basically it moves one character from string to prefix, and it does all combinations to get all permutations.
Then it runs then next loop, which gives:
"12" "34"
"13" "24"
"14" "23"
"21" "34"
.. etc
Eventually you will get:
"123" "4" -> "1234"
"124" "3" -> "1243"
.. etc
As for you problem: note that it will be inefficient for numbers like 777 it will just give you 777 6 times. Also, you need rotations, not permutations. 197 should give 197,971,719 but not 179,791,917

Special HTML JSON parsing

i've got an JSON String like this:
a: 2: {
s: 4: "unit";
s: 2: "h1";
s: 5: "value";
s: 40: "Mercedes-Benz A 45 AMG / 340 PS / Allrad";
}
Now, I don't know what to do with that because of the weird keys (i think in the 's' that means the length of the string).
If I use it like this nothing can parse it (with php json_decode or with obj-c SBJsonParser).
Is there a way to generate html of that in php or how can i read only the last value?
Thanks in advance
This looks like serialized array, but your string is definitely not valid. (http://php.net/manual/en/function.unserialize.php)
This is not a valid JSON so no json parser would read it, you can treat it as a regular string and use string manipulation to get what you need.
In PHP in order to get the last value:
$str = ...//your json string
$lastValue = substr($str,strripos($str,'s:'));
this should return (according to your sample input): "40: "Mercedes-Benz A 45 AMG / 340 PS / Allrad";"
The above mentioned JSON format is absolutely invalid.
{
"a": {
"2": [
{ "s": { "4": "unit" } },
{ "s": { "2": "h1" } },
{ "s": { "5": "value" } },
{ "s": { "40": "Mercedes-Benz A 45 AMG / 340 PS / Allrad" } }
]
}
}
This would be a valid JSON format
I suggest to treat it as a string for the beginning. Then use string manipulation to get rid of the "a: " and "s: ". Other requests may return other types (if I am right that these letters indicate a data type.)
Then you could try parsing it with a JSON parser.
No guarantee though :) Because I am not qute sure out of the top of my head, whether these numbers are valid JSON or have to be encapsulated in quotation marks.

Categories