Uson jsTree with custom JSON structure - json

I've been searching for ways to do this, but I'm missing something.
I have a custom JSON object, which can basically have any structure, and I want to use this with jsTree. I've found a corresponding plugin here, that claims to have a _parse_json function to transform a custom JSON object to the format that jsTree requires, but for the life of me I can't figure out how to call that plugin. It seems to be included in the version of jsTree that I'm using (1.0rc3).
There is a usage of UIMTreeProcessor I've found, that consists of parsing the XML and calling jsTree like so:
$.jstree._themes = "Content/jstreethemes/";
this.treeEl.jstree({
"json_data" : {
"data":data,
"progressive_render":"true"
},
"plugins" : [ "themes", "ui", "json_data" ],
"core": { "animation": 0 }
});
Now, instead of UIMTreeProcessor parsing the XML and populating data, I want to call $.json_data._parse_json(), but I keep getting errors that this function doesn't exist.
Can anyone show me an example? Thank you kindly.

Related

How to save a JSON-object and use it in another request?

i'm currently trying to set up some JMeter testplans. I am pretty new to this (started a week ago) and don't have much programming experience either, so i hope you could help me in this case.
I already set up some http requests and have some simple JSON Extractor post processors to save some of the variables and use them with the "${variable}" notation.
But now i need to save and modify an object from a response to use that in the next http request.
My respose is a extremely big JSON object and the part im interested in looks something like this:
{
"payload": {
"workspace": {
"resultInstance": [
[{"part": "1"...}],
[{"part": "2"...}],
...
[{"part": "20"...}]
]
}
}
}
Note that for whatever reason these objects {"part":"1"...} are inside a nested array. And they are also pretty big.
I would like to safe those objects in a variable to use them in my next http request which should looks like this:
{
"instanceChange": {
"functionChecks": [
{"part": "1"...},
{"part": "2"...},
...
{"part": "20"...}
]
}
}
So what im really trying to find is a way to save all of the possible objects inside the nested array "resultInstance" and put them inside the non nested array "functionChecks".
I already looked inside the JMeter documentation but because of my poor programming background i cant find a way to realize this.
I think i need something like the JSR223 PostProcessor and "simply go through the resultInstance-array and use smth. like an getObject() on these", but i cant figure out the code i need and if its even possible to safe objects in variables in Jmeter.
Im pretty thankful for every bit of help or advice :).
Thanks in advance,
aiksn
Add JSR223 PostProcessor as a child of the request which returns the JSON response
Put the following code into "Script" area:
def response = new groovy.json.JsonSlurper().parse(prev.getResponseData())
def request = ['instanceChange': ['functionChecks': response.payload.workspace.resultInstance]]
vars.put('request', new groovy.json.JsonBuilder(request).toPrettyString())
That's it, you should be able to refer the generated request body as ${request} where required
More information:
Apache Groovy - Parsing and producing JSON
Apache Groovy - Why and How You Should Use It
let response ={
"payload": {
"workspace": {
"resultInstance": [
[{"part": "1"...}],
[{"part": "2"...}],
...
[{"part": "20"...}]
]
}
}
};
let requestObj={
"instanceChange": {
"functionChecks": [
]
}
};
response.payload.workspace.resultInstance.forEach(myFunction);
myFunction(item, index) {
requestObj.instance.functionsCheck.push(item[0]);
}

d3js forced directed cannot read from json

I have a simple json file which is :
{
"nodes":[
{"name":"Moe","group":1},
{"name":"Madih1","group":1},
{"name":"Madih2","group":1},
{"name":"Nora","group":1},
{"name":"Myna","group":1}
],
"links":[
{"source":35,"target":44,"value":1},
{"source":44,"target":35,"value":1},
{"source":45,"target":35,"value":1},
{"source":45,"target":44,"value":1},
{"source":35,"target":49,"value":1},
{"source":49,"target":35,"value":1}
]
}
when I save it use exactly the html code as shown in http://bl.ocks.org/4062045#index.html and address the above json, nothing appears on the cancas.
I appreciate it if you help me with this one as I am not very familiar with it. Moreover, it would be great if I know the minimum code required for drawing a graph like this using json.
Best,
The number of "source" and "target" refer to the index of the item in nodes array.
So you can change your json to following:
{
"nodes":[
{"name":"Moe","group":1},
{"name":"Madih1","group":1},
{"name":"Madih2","group":1},
{"name":"Nora","group":1},
{"name":"Myna","group":1}
],
"links":[
{"source":0,"target":1,"value":1},
{"source":1,"target":2,"value":1},
{"source":2,"target":3,"value":1},
{"source":3,"target":4,"value":1},
]
}
Then you can just copy the codes from http://bl.ocks.org/4062045#index.html example as the minimum code.
Remenber to change the json file to your own json file.
d3.json("path/to/your/json", function(error, graph) {
//codes
});

working with JSON data, trying to parse nested data

I just working with JSON data and am playing around with jQuery and Ajax requests. Pretty basic stuff, but here's my problem.
I have a basic data set which I was using for time tracking. I know how to parse the simple JSON data like this:
{
"end" : "1/18/2011",
"start" : "1/18/2011",
"task" : "Code Review",
},
It's the more complicated stuff I'm trying to parse like this where I'm trying to pull the "time" data out.
{
"end" : "1/17/2011",
"start" : "1/17/2011",
"task" : "Exclusive Brands",
"time" : {
"analysis" : 4,
"documentation" : 3,
"meetings" : 2
}
This is the code for the script I've been using to parse the simple data:
$(function() {
$('.load').click(function(){
$.getJSON("data.js",function(data){
$.each(data.timesheet, function(i,data){
var div_data ="<div class='box'>"+data.start+" "+data.task+"</div>";
$(div_data).appendTo("#time-tracking");
});
}
);
return false;
});
});
My question is what's the format to parse the time data, or what's the best way to parse the information nested inside the time element?
Any help will be greatly appreciated.
A JSON string will be parsed into an object. When parsed, the time is the key of one object. You could retrieve the value of this object through the dot operator (.).
data = JSON.parse('{"end":"1/17/2011", "start":"1/17/2011", "task":"Exclusive Brands", "time": {"analysis":4, "documentation":3, "meetings":2 } }')
// => obj
data.time.analysis
// => 4
In your case similarly you could use the data.time.meetings to access your data from remote server.
Unless I am terribly mistaken, since jquery already converted data into a javascript for you, you should be able to access time as if it was a javascript object like so:
var analysis = data.time.analysis;
var documentation = data.time.documentation;
var meetings = data.time.meetings;
etc...

Converting velocity response to JSON

I am using struts 2 and velocity templates to generate JSON response.
Now the catch is the response is not generated using some velocity JSON plugin
it's just a String that comes out once velocity is done with its parsing and rendering of
response, and on client side I do eval to get the response from string to JSON.
What I really need is some solution on velocity's or struts' side where, once the result is
generated by velocity, the framework should call my API where I can convert the response output of vm file into JSON using my own logic. How do achieve this?
For example:
On browser using JavaScript I have designed a tree widget that I use for displaying comments in tree structure.
Say user clicks on comments button.
My UI widget will fire an AJAX to get data for comments.
This request is intercepted by STRUTS 2 framework.
It will call, say, getComments() action API and will populate an arrayList with comment object say cmt.
Now the response is handled by a velocity template(*.vm).
Now in vm I am writing code like this:
{ "CommentsData" : [
#set($sep="")
#foreach($c in $cmt)
$sep
{
"commentText" : $c.getText()
}
#set($sep=",")
#end
}
Now the final response may turn out like this:
{ "CommentsData" : [
{
"commentText" : "This is comment 1"
},
{
"commentText" : "This is comment 2"
},
{
"commentText" : "This is comment 3"
},
{
"commentText" : "This is comment 4"
}`
]
}
Now this may look like JSON, but its not strict JSON; I mean if I miss
some , somewhere then on client side in JavaScript my eval might fail or JSON.parse()
will fail, but on velocity template I have now clue if JSON is malformed.
So once the above velocity template is generated I need some control, where I can write some Java code to do some validations on the response.
I see that my approach to use velocity template to generate JSON output (actully a String that looks like JSON) may be wrong. But still I need to handle the response of every velocity template I have written.
Not sure how you are using velocity. We don't use velocity when outputting JSON; we just create a JSON convertible object and output it directly from controllers using response.write(jsonObject.toJson()). This way, proper JSON is always generated.

Provide JSON as data for rich:extendedDataTable

I am implementing a full text search in my project and for that the result of search comes from SOLR server as JSON.
Is it possible to provide JSON for data or value attribute of the richfaces component extendedDatatTable or simple dataTable.
Resultant JSON would look something like :
`resultObj = {
"numberOfResults":25,
"results":[
{
"instanceId":100,
"inj_Name":"Inj4",
"i_IdentificationNumber":127,
"noz_Name":"Nozzle4",
"n_IdentificationNumber":460,
"thr_Name":"Throttleplate4",
"t_IdentificationNumber":0,
"act_Name":"Atuator4",
"a_IdentificationNumber":781
},
{
"instanceId":100,
"inj_Name":"Inj4",
"i_IdentificationNumber":127,
"noz_Name":"Nozzle4",
"n_IdentificationNumber":460,
"thr_Name":"Throttleplate4",
"t_IdentificationNumber":0,
"act_Name":"Atuator4",
"a_IdentificationNumber":781
} ]
};
`
Regards,
Satya
As far as I know, no you can't. Your best bet would be to reflect this data into a List of Java objects using Gson and then use that list as a data source.