Extjs how to decode a json string? - extjs

I have to decode JSON with Extjs 4:
I have used Ext.decode(string, true), but it doesn't work 'cause my string is a JSON with a JSON string (escaped) inside... like this:
var string = '{
success: true,
rows: [{
"id": 33,
"defaultset": 1,
"name": "Generico",
"jsonfields": "[{\"name\":\"cm:addressees\",\"title\":\"Destinatari\",\"description\":\"Destinatari\",\"dataType\":\"d:text\",\"url\":\"\/api\/property\/cm_addressees\"}]",
"eliminato": 0
}]
}';
as you can see the field jsonfields is a JSON string. When I use
Ext.decode(string, true);
nothing happens neither error.
Any suggestions?

You may try like this:
var string = '{success:true, rows:[{"id":33,"defaultset":1,"name":"Generico","jsonfields":"[{\\"name\\":\\"cm:addressees\\",\\"title\\":\\"Destinatari\\",\\"description\\":\\"Destinatari\\",\\"dataType\\":\\"d:text\\",\\"url\\":\\"/api/property/cm_addressees\\"}]","eliminato":0}]}';
var decodedString = Ext.decode(string);
console.log(decodedString);
that's a little bit tricky. If you remove safe parameter you will see that your json misses \ in your jsonfields thats because your string is in ' quotes and one \ does the job for it but you want something different... so you have to double it.
fiddle example

It does work, for example I am getting my Json from the server,
websocket.onmessage = function(event)
from the websocket actually and later when I want to decode my json,
var json = Ext.decode(event.data);
and where I need my string for example
json.map.MessageType
My json looks like this:
mpty":false,"map":{"MessageText":"Ciao, how are you?","MessageType":"IN"},"hashtable":{"MessageText":"Ciao, how are you?","MessageType":"IN"},"persistData":{"MessageText":"Ciao, how are you?","MessageType":"IN"}}
Hope this helps, cheers!

Related

Gatling - Extract Json value from previews request

I'm new in Gatling, and trying to get value from json key.
This check give me labels:
.check(jsonPath("$[1].ppp[1].label").saveAs("label"))
In this request i got all json data like this
[
{
"id":258,
"code":"D00CC3056",
"label":"Test-0"
},
{
"id":260,
"code":"D00RR148",
"label":"Test-1"
}
]
From this Json, i need to get code value from a specific label, to use it in request after.
Something like : get code where label is "Test-1".
I've trying this but doesn't works:
.exec(
http("request_3:GET_/api/code")
.get("/api/code")
.check(status.is(200))
.check(jsonPath("$..[*].code").find("{$label}").saveAs("code"))
)
I don't know how to do this.
Any help ?
With JMESPath that you should really learn and that Gatling supports:
.check(jmesPath("[?label == 'Test-1'].code").saveAs("code"))

how to concat two json objects in angularJs

I have the following in an angular controller:
var jsonOne = $cookies.get('test');
// console logs {"domain":"localhost","zip":33333}
var jsonTwo = angular.toJson($scope.formData);
// console logs {"id":"210","name":"sam"}
var final = $.extend(jsonOne, jsonTwo);
// console logs [object object]
My goal is to get:
var final
// console logs {"domain":"localhost","zip":33333, "id":"210","name":"sam"}
I'm asking under angular because when I try it in a static html document it works fine, but for some reason it wont work in angular. Any ideas?
The issue seems to be your use of angular.toJson, which from the docs serializes inputs as a JSON string. Using a string with angular.extend() won't work as you want.
Try to retrieve your elements without serializing them as a string - it seems like you might already have the objects and don't need to use .toJson.
Once you have your two objects, use angular.extend() to combine them.
var first = { "A": "B" };
var second = { "C": "D" };
var third = angular.extend(first, second);
console.log(third);
// in chrome it logs { A: "B", C: "D" }

Convert json string to array in php

Im using curl the retrieve a json string from a log file. I was able to retrieve the string but it comes in this format.
[{"candy":"lollipop","timestamp":1385504260,"color":"red"}]
[{"candy":"laffytaffy","timestamp":1385504260,"color":"blue"}]
When i try to convert it to an array using decode, its as if decode isn't working. I was hoping someone could assist me in figuring this out.
First, this json seems to be wrong. It should has this format.
[{"candy":"lollipop","timestamp":1385504260,"color":"red"},
{"candy":"laffytaffy","timestamp":1385504260,"color":"blue"}]
Maybe it's the problem.
#Carlos is right your JSON is invalid. You have 2 separate arrays instead of an array of objects.
It should look like this instead:
[
{
"candy": "lollipop",
"timestamp": 1385504260,
"color": "red"
},
{
"candy": "laffytaffy",
"timestamp": 1385504260,
"color": "blue"
}
]
Try like this
<?php
$jsonData = '[{"candy":"lollipop","timestamp":1385504260,"color":"red"},
{"candy":"laffytaffy","timestamp":1385504260,"color":"blue"}]';
$decodedJson = array();
$decodedJson = json_decode($jsonData);
print_r ($decodedJson);
?>

Restangular - Get object w/id, edit object, update object

I can't seem to figure this out -
I want to do something like:
content = Restangular.one('contents', 2).get()
content.name = "Chunky Bacon"
content.put()
But this doesn't work - content doesn't have a put function, and interestingly, it doesn't have a name, either (is that because it's a "promise"?)
Now, the Restangular docs do describe updating an object, but you get that object as part of a collection:
content_id2 = Restangular.all("contents").getList()[2]
content_id2.name = "Chunky Bacon"
content_id2.put()
And this works. But! Not only have I fetched the entire list, I can't figure out how to easily get my content by it's ID; something like Restangular.all("contents").getList().find(2)
customPUT(..) seems like the next best bet:
Restangular.one("contents", 2).customPUT("", {"content" : {"name" : "Chunky Bacon"} } )
Only when the Rails server receives it, params["content"] == {id: 2} - it seems like something on the client side is stomping that value. (Again, note, no name field). Now, I can work around it simply by doing something like:
Restangular.one("contents", 2).customPUT("", {"content_data" : {"name" : "Chunky Bacon"} )
but it really seems like I'm just missing how you're supposed to do this.
Alright, found it, thanks to this question:
#In the beginning:
Restangular.one("content", 2).get().then(function(c){
$scope.content = c
})
#Elsewhere:
$scope.content.name = "Chunky Bacon"
#Finally:
$scope.content.put()

Can i return an array of "number, number" in json format?

i have a list of lat/long objects on my server.
eg.
public class LatitudeLongitude
{
public float Latitude;
public float Longitude;
}
simple.
now, can i return a collection of these, in json format .. BUT ... i do not want to list the key, just the values.
This means the normal result would be something like ...
{ { lat: 111, long : 222 }, { lat: 333, long : 444 } } ..
but i'm hoping for...
{ {111, 222}, {333, 444} ..... }
{ {obj1.Lat, obj1.Long}, {obj2.Lat, obj2.Long} ... etc. ... }
Is this possible? I mean, i can make that string on the server side easily. But is that a correct JSON output format?
Can someone please confirm, etc.
cheers :)
PS. I hardly know any Json, so please don't hesitate to correct my poor examples above.
Use JSON arrays:
[ [111, 222], [333, 444], ... ]
Ayman's proposal isn't quite correct JSON syntax. As an object {..} is always composed of fields having a name. So we again would have object with 2 fields of type 'array' that don't have a name -> not valid json for an object.
There is a very good description of how a json string can look like at www.json.org. Looking at this description you'll see an object is not allowed without a name String for a field so this {111,222} is not a valid object in json, but this is valid [111,222] array. This is not a valid object {[111,222]} but this is {array1 : [111,222]}.
I guess what you actually want is most likely an array of arrays like this not encapsulated as an object.
[[111,222],[333,444]].
This approach would give you the choice to add as much arrays of lat/long as you like to the enclosing array and at the same time you can process the inner arrays no matter what size the outer array is with an javascript loop.
Hope that helped.
cheers
Michael
If you need to access it using the keys in JSON, you'll need to specify it (or use a JS to convert arrays to objects on the client side). Otherwise, arrays should work:
var json = [ [123, 456], [234, 567] ];
var convertedJson = [];
for (i = 0; i < json.length; ++i) {
var thisObj = new Object();
thisObj.latitude = json[i][0];
thisObj.longitude = json[i][1];
convertedJson[i] = thisObj;
}
But is that a correct JSON output
format?
You can paste in JSON at jslint.com to validate your JSON
The ASP.NET MVC JSON serializer should convert a IList<LatitudeLongitude> to something like this:
[{"Latitude":111,"Longitude":222},{"Latitude":333,"Longitude":444} .. ]
I believe that under the ASP.NET MVC JSON serializer is just a hot-wired .NET core Javascript serializer.
I dont think it's possible from .NET using a serializer. Prob need to render the string manually.

Resources