Rectangle 27 1

I think the problem is that you are trying to json_encode a string that is already json encoded in your php code. Typically you call json_encode on a php array to reformat it as a json string. since it's already a string i believe you can simply echo($json); instead of trying to json_encode it.

php - Android org.json.JSONException: Cannot convert to JSON array - S...

php android json http
Rectangle 27 1

you have following issues in current json String:

1. you will need to remove spaces from Json String in PHP code before passing it to json_encode. valid json string is:

$json = '[
{"firstName":"John","lastName":"Doe"}, 
{"firstName":"Anna","lastName":"Smith"}, 
{"firstName":"Peter","lastName": "Jones"}
]';
print $json;

2. no need to add new line character when reading data from BufferedReader :

String line = null;
while((line = reader.readLine()) != null){
  sb.append(line);  //<<< remove \n from here
}

still not working :(

@DrDebug : still not working mean? can u plz update your latest code and log if u are getting any error

php - Android org.json.JSONException: Cannot convert to JSON array - S...

php android json http
Rectangle 27 30

1. Convert it to a JSON string, then parse it in PHP (recommended)

You can do 2 things:

var json_arr = JSON.stringify(arr);
$arr = json_decode($_POST['arr']);
// Use <#> or any other delimiter you want
var serial_arr = arr.join("<#>");

PHP

$arr = explode("<#>", $_POST['arr']);

the problem is that the array contains lines of REAL text, with spaces and punctuation marks. I don't want to mess it up.

When you encode and parse it with JSON, data isn't lost. Give it a try ;)

If you are using asp.net with automatic mapping or something similar, then @Curious answer is what you need.

javascript - appending array to FormData and send via AJAX - Stack Ove...

javascript ajax arrays form-data
Rectangle 27 28

1. Convert it to a JSON string, then parse it in PHP (recommended)

You can do 2 things:

var json_arr = JSON.stringify(arr);
$arr = json_decode($_POST['arr']);
// Use <#> or any other delimiter you want
var serial_arr = arr.join("<#>");

PHP

$arr = explode("<#>", $_POST['arr']);

the problem is that the array contains lines of REAL text, with spaces and punctuation marks. I don't want to mess it up.

When you encode and parse it with JSON, data isn't lost. Give it a try ;)

If you are using asp.net with automatic mapping or something similar, then @Curious answer is what you need.

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

javascript - appending array to FormData and send via AJAX - Stack Ove...

javascript ajax arrays form-data
Rectangle 27 212

You can quickly convert deeply nested objects to associative arrays by relying on the behavior of the JSON encode/decode functions:

$array = json_decode(json_encode($nested_object), true);

This is the best solution if you want a full depth recursive conversion (and of course dont mind the bad performance)

Convert PHP object to associative array - Stack Overflow

php arrays
Rectangle 27 1

...
String result = EntityUtils.toString(entity);

JSONObject json = (JSONObject ) new JSONTokener(result).nextValue();
json.getString('name');
...

Also check the PHP script encoding. I had similar problem. I set encoding to UTF-8 with DOM in text editor (Notepad++,..) and it start working.

php - org.json.JSONException: Value Array of type java.lang.String can...

java php android json
Rectangle 27 9

There are three solution to this problem:

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

How to convert json encoded PHP array to an array in Javascript? - Sta...

php javascript ajax arrays json
Rectangle 27 9

There are three solution to this problem:

How to convert json encoded PHP array to an array in Javascript? - Sta...

php javascript ajax arrays json
Rectangle 27 1

resorts
room_prices
strings
// Storing each json item in variable
String name = resorts.getString(i);
String price = rates.getString(i);
JSONObject c = resorts.getJSONObject(i);
JSONObject d = rates.getJSONObject(i);

// Storing each json item in variable
String name = c.getString(TAG_NAME);
String price = d.getString(TAG_ROOM_PRICE);
...........
        ...................

        if (success == 1) {
            // resorts found
            // Getting Array of resorts
            resorts = json.getJSONArray("resorts");
            rates = json.getJSONArray("room_prices");

            int i;

            // looping through All resorts
            for (i = 0; i < resorts.length() & i < rates.length(); i++) {
                // Storing each json item in variable
                String name = resorts.getString(i);
                String price = rates.getString(i);

                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                map.put(TAG_NAME, name);
                map.put(TAG_ROOM_PRICE, price);

                // adding HashList to ArrayList
                resortsList.add(map);
            }

        }

php - org.json.JSONException: Valuef type java.lang.String cannot be c...

java php android json
Rectangle 27 4

You don't need to convert the array data to a JSON string yourself, use the Laravel $casts parameter on your model: https://laravel.com/docs/5.2/eloquent-mutators#attribute-casting

You should do something like this in your Div model:

protected $casts = [
    'divisions' => 'array',
];
$options['key'] = 'value';

Here array means prepare key value pair in array like array('key' =>'value')

php - Laravel : How to store json format data in database? - Stack Ove...

php mysql json laravel
Rectangle 27 6

You can use json_encode function to convert arbitrary data into JSON. Assuming that you want to return an array of strings, here is how you will use json_encode:

<?php
    include_once '../include/lib.php';
    $res = array(); // initialize variables
    $sqlgr = sprintf("
        SELECT ACT_ID
        FROM PRIVILLAGE
        WHERE MAINGR_ID=%d
        ",
        $_POST['gr_name']
    ); // only select those columns that you need
       // and do not trust user input
    $resultgr = sql($sqlgr);
    while($rowgr = mysql_fetch_array($resultgr)){
        $res[] = $rowgr['ACT_ID'];
    }
    echo json_encode($res); // use json_encode to convert the PHP array into a JSON object
                            // this will output something like ['foo', 'bar', 'blah', 'baz'] as a string
?>

On the client side you can use jQuery.post method, like this:

<script type="text/javascript">
$("#group_name").change(function () {
    $.post("selectedgr.php", {
        gr_name: $(this).val()
    }, function (data) {
        // console.log(data);
        // jQuery will convert the string "['foo', 'bar', 'blah', 'baz']" into a JavaScript object
        // (an array in this case) and pass as the first parameter
        for(var i = 0; i < data.length; i++) {
            $("#" + data[i]).attr("checked", "checked");
        }
    }, "json");
});
</script>

php - Jquery .post() return array - Stack Overflow

php jquery arrays json .post
Rectangle 27 6

You can use json_encode function to convert arbitrary data into JSON. Assuming that you want to return an array of strings, here is how you will use json_encode:

<?php
    include_once '../include/lib.php';
    $res = array(); // initialize variables
    $sqlgr = sprintf("
        SELECT ACT_ID
        FROM PRIVILLAGE
        WHERE MAINGR_ID=%d
        ",
        $_POST['gr_name']
    ); // only select those columns that you need
       // and do not trust user input
    $resultgr = sql($sqlgr);
    while($rowgr = mysql_fetch_array($resultgr)){
        $res[] = $rowgr['ACT_ID'];
    }
    echo json_encode($res); // use json_encode to convert the PHP array into a JSON object
                            // this will output something like ['foo', 'bar', 'blah', 'baz'] as a string
?>

On the client side you can use jQuery.post method, like this:

<script type="text/javascript">
$("#group_name").change(function () {
    $.post("selectedgr.php", {
        gr_name: $(this).val()
    }, function (data) {
        // console.log(data);
        // jQuery will convert the string "['foo', 'bar', 'blah', 'baz']" into a JavaScript object
        // (an array in this case) and pass as the first parameter
        for(var i = 0; i < data.length; i++) {
            $("#" + data[i]).attr("checked", "checked");
        }
    }, "json");
});
</script>

php - Jquery .post() return array - Stack Overflow

php jquery arrays json .post
Rectangle 27 4

http://api.jquery.com/jQuery.getJSON/

$.getJSON('ajaxfetch.php', function(data) {
  var locations = []; 

  $.each(data, function(key, val) {
    locations[val.deviceID] = [];
    locations[val.deviceID].push(val.id);
    locations[val.deviceID].push(val.latitude);
    locations[val.deviceID].push(val.longitude);
  });
});

Not 100% tested but it's along the right lines. Unsure where you get the location name from as it's not in the array so I used deviceID. Using getJSON should make your life easier.

How to convert json encoded PHP array to an array in Javascript? - Sta...

php javascript ajax arrays json
Rectangle 27 4

http://api.jquery.com/jQuery.getJSON/

$.getJSON('ajaxfetch.php', function(data) {
  var locations = []; 

  $.each(data, function(key, val) {
    locations[val.deviceID] = [];
    locations[val.deviceID].push(val.id);
    locations[val.deviceID].push(val.latitude);
    locations[val.deviceID].push(val.longitude);
  });
});

Not 100% tested but it's along the right lines. Unsure where you get the location name from as it's not in the array so I used deviceID. Using getJSON should make your life easier.

How to convert json encoded PHP array to an array in Javascript? - Sta...

php javascript ajax arrays json
Rectangle 27 7

Convert your PHP array to JSON, using json_encode.

Now you have a JavaScript object you can use.

Hmmm! JSON. '...a text based open standard designed for human-readable data exchange' from our wiki friend.

Hmmm! JSON seems to be the popular answer. JSON '...a text based open standard designed for human-readable data exchange' (from our wiki friend). This seems to have possibilities - much better than those other possible solutions I found here for what I need to be able to do. Now if I could only figure out what and how? Don't suppose there is an argo() function? Much more research needed. Thank you all for your very, very, prompt help. Take care. Mike

Wow! Just took a look at the output of a json_encode() of the PHP array. Good stuff! Thank you all again. Mike

Passing php array to javascript - Stack Overflow

php javascript google-maps
Rectangle 27 1

In PHP, there is no predefined datatype as json if you passing json in data; PHP will consider it as string. So, declare array and convert into json using json_encode and it will work.

Give it a try.

$data = [
  "date"=> "2017-02-22",
  "identifier" => "1337",
  "lines" => [  "test" => "this is a test" ],
  "name"=> "Wolf"
];
$json = json_encode($data);

$register_sale = curl($url,'POST', $json);

Now its not giving Invalid JSON....Bad Json its something ....some values required in json might be u not sending

The string im testing with in code is the same im testing with using the testingtool of the api, where it works, thats why i find it strange.

php - How to pass JSON in CURL request using POST - Stack Overflow

php php-curl
Rectangle 27 1

In PHP, there is no predefined datatype as json if you passing json in data; PHP will consider it as string. So, declare array and convert into json using json_encode and it will work.

Give it a try.

$data = [
  "date"=> "2017-02-22",
  "identifier" => "1337",
  "lines" => [  "test" => "this is a test" ],
  "name"=> "Wolf"
];
$json = json_encode($data);

$register_sale = curl($url,'POST', $json);

Now its not giving Invalid JSON....Bad Json its something ....some values required in json might be u not sending

The string im testing with in code is the same im testing with using the testingtool of the api, where it works, thats why i find it strange.

php - How to pass JSON in CURL request using POST - Stack Overflow

php php-curl
Rectangle 27 4

Problem lies in format of your date strings in JSON. Format is not compatible with date formats for plugin. If you convert to UNIX timestamps using strtotime() in php they will work.

php - Not able to feed full calendar with json events - Stack Overflow

php jquery json fullcalendar
Rectangle 27 2

It looks like you've got a JSON string and want to convert it to an array. There are a few ways to do this. You could use explode like this:

or you could use json_decode like this:

$bar = '["","","","",""]';
$new_str_array = json_decode($bar);

foreach($new_str_array as $arr){

    echo $arr;

}

arrays - PHP Comma Delimiter or Explode for Foreach Loop - Stack Overf...

php arrays foreach delimiter explode
Rectangle 27 13

You should probably return errors (which is an instance of Illuminate\Support\MessageBag) and encode that. A MessageBag instance allows you to convert it directly to its JSON representation.

$errors = $validation->errors();

return $errors->toJson();

Now not to toot my own horn but I've recently developed a RESTful API package for Laravel which does all of this for you and all you need to do is throw a simple exception. See my dingo/api package and the Wiki on returning errors. Basically, instead of returning the errors you would throw an exception.

throw new Dingo\Api\Exception\StoreResourceFailedException('Could not create a new user.', $validation->errors());

It would be represented by the following JSON.

{
    "message": "Could not create a new user.",
    "errors": {
        "username": ["The username is already in use."]
    }
}

ill try your package after i make this work. the thing is it returns the if($validation->fails()){ } in to html in my terminal so you would end up returning some html then it wont read the next lines of codes. that my theory on what happening to it..

fount it. i have to specify the content type. i found the answer here stackoverflow.com/questions/7172784/

You should toot your horn. Dingo is a very popular package now. So thank you for making it!

php - REST API in Laravel when validating the request - Stack Overflow

php validation rest laravel-4