Backbone handling json array vs json object - json

How should I handle json returned as array in Backbone.Collection's overridden parse?
I used the following for json returned as object:
parse: function(response) {
return response.results;
}
What about when json gets returned as array?

Backbone, by default, expects an array. If the array is nested within an object, you need to over-ride parse() as you have done above. However, if the response returns an array, you don't need to do anything (since that is what backbone is expecting). Finally, if you are expecting a mix of the two (that is, might be an array sometimes and might be an object the other times, see dbaseman's answer).

Related

Difference between toArray() and json_decode() for Laravel Blade

When simply returning $users, I get the exact same result with toArray() and json_decode().
But when looping $users in my view, I get an error ('Trying to get property 'xxx' of non-object) with the toArray() method. Not with json_decode(). Any idea why? I'm on Laravel 6.
$client = HttpClient::create();
$response = $client->request('GET', 'https://jsonplaceholder.typicode.com/users');
$users = json_decode($response->getContent());
// $users = $response->toArray();
return $users;
The toArray() method return pure array. While json_decode() keys are always strings, while the value can be a string, number, true or false, null or even an object or an array.
Therefore, you are trying to access an object returned by json_decode which works if you have objects while encoding it. But, while trying to access an object from array perspective it will not work because arrays are plain array.
Explanation of Json_decode()
PHP's json_decode function takes a JSON string and converts it into a PHP variable. Typically, the JSON data will represent a JavaScript array or object literal which json_decode will convert into a PHP array or object.
Explaining PHP array is beyond this scope. However:
An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.
Finally,
You are trying to access an object returned by json_decode - works fine. But an array will not hold objects such that it throws that trying to get property of non-object.

Add/Remove elements in a JSON Array in Scala/Play

I have a pretty complex JSON object that contains, among other things, some JSON arrays that I need to update, removing and adding elements.
To do that I'm trying to use a JsPath that point directly to the object inside the array that I need to remove, something like:
/priceLists(1)/sections(0)/items(0)
to remove the element I tried to use json.prune and it doesn't work, I get this error: error.expected.jsobject
Would would be the best way to do that?
Your question is lacking a precise context (i.e., structure of your json data), but let's do with what we have.
The error message you get is clear, you can only call prune on a json object, to prune one of its values. You can't use it to prune an element of a json array.
I can only advise you to use json.update, stating that like prune, update only works on json objects. In the body of the update, work on your arrays as you usually do with scala/java data types.
__.json.update(__.reads[JsArray].map { jsArray =>
val removedElement = JsArray(jsArray.value.filter(_ == ???))
val addedElement = removedElement :+ JsBoolean(true)
addedElement
})

Deserialize a string to json

I want to know whether deserialize converts json to string or string to json.I need my string to be returned as Json so i used deserialize, but unsure about its syntax.Can anyone direct me correctly.
My code
JavaScriptSerializer datajson = new JavaScriptSerializer();
var objec = datajson.Deserialize<string>(data);
return Json(objec,JsonRequestBehavior.AllowGet);
Serialisation is the act of taking objects and turning them into something more persistable or communicable, i.e. turning objects into JSON, XML or binary data.
Deserialisation is the act of taking serialised data and turning it back into objects.
So in your case, if you want to turn your objects/variables into JSON, the process is called serialisation.
Your code, assuming it is MVC C# (you may wish to add these tags to your original post), appears to be deserialising a JSON encoded string into a string, then serialising it back to JSON again when it returns the view. I'm not sure why you would want to do this. You should be able to simply do:
return Json(data, JsonRequestBehavior.AllowGet);

Parse JSON object w/o using identifier

I get an array of objects returned from a AJAX request in the form:
[Object { M_ID_mt="9"}, Object { M_ID_mt="12"}, 5 more...]
But because the identifier (M_ID_mt in this example) will change depending on the data returned, I want to parse this array in a loop without using the identifier.
Is there a way to pull the "data" (9, 12, ...) from each object w/o dereferencing using the identifier?
If you really receive JSON, such as
[{"M_ID_mt":"9"},{"M_ID_mt":"12"}]
there is libraries to parse/query json, may be one would do the job:
http://jsonselect.org
http://www.jsoniq.org/
https://github.com/mmckegg/json-query
...

Struts:JSON:return multiple objects

Is it possible to return multiple JSON objects in the request header with Struts1? I am presently returning a single JSON objects, however the need now is to return a second data structure. All the client-side processing works perfectly for the single data structure in the single JSON objects, I really do not want to complicate it by putting two hetrogenous data structures in a single return JSON object. tia.
I don't know struts or why you can't return multiple JSON objects, but if you genuinely can't, why don't you return a list of your objects? you can unbox them at the receiving end.
in other words, if you were previously returning obj = {...}, and now need to return that as well as obj2 = {...}, you can return [obj, obj2].
maybe this doesn't solve your problem, but it'll get you around it pretty easily.