Json Jackson List of List - json

Is this even possible?
I'm trying to get a set of coordinates for a chart and i'm returning a list of list.
i call the method with $.ajax and i get a syntax error , but when i call the method in a webpage i can get the results and then i validate them with JSONLint and says it's ok, then i use JSONLint with my localhost url and it says that theres a syntax error but i cant understand it, can anyone help me?
The method
#RequestMapping(value="graph1")
public #ResponseBody JsonPack<List<GraphData>> graphAge_Weight0_36(#RequestParam(value="patient")int idPatient){
List<Ageweight0To36Months> data = aw036Service.getAll("Ageweight0To36Months");
List<List<GraphData>> allData = new ArrayList<List<GraphData>>();
List<GraphData> list = new ArrayList<GraphData>();
Ageweight0To36Months aux = new Ageweight0To36Months();
GraphData singleD = new GraphData();
Patient patient = patientService.getById(idPatient);
String query = "FROM Consultation c where c.idPatient=" + idPatient;
String gender = (patient.getSex().compareTo("masculino")) == 0? "M" : "F";
for(Method m: aux.getClass().getMethods()){
if(m.getName().startsWith("getP") && m.getParameterTypes().length == 0){
list = new ArrayList<GraphData>();
for(Ageweight0To36Months aw: data)
{
System.out.println(aw.getGender() +","+gender);
if(aw.getGender().compareTo(gender) == 0){
try{
Object p = m.invoke(aw);
singleD = new GraphData(aw.getAgeInMonths(),Double.valueOf(p.toString()).doubleValue());
list.add(singleD);
}catch (Exception e){
e.printStackTrace();
}
}
}
allData.add(list);
}
}
The response
{"aaData":[[{"xCoordinate":0.0,"yCoordinate":2.355451},{"xCoordinate":0.5,"yCoordinate":2.799549},{"xCoordinate":1.5,"yCoordinate":3.614688},{"xCoordinate":2.5,"yCoordinate":4.342341},{"xCoordinate":3.5,"yCoordinate":4.992898},{"xCoordinate":4.5,"yCoordinate":5.575169},{"xCoordinate":5.5,"yCoordinate":6.096775},{"xCoordinate":6.5,"yCoordinate":6.56443},{"xCoordinate":7.5,"yCoordinate":6.984123},{"xCoordinate":8.5,"yCoordinate":7.361236},{"xCoordinate":9.5,"yCoordinate":7.700624},{"xCoordinate":10.5,"yCoordinate":8.006677},{"xCoordinate":11.5,"yCoordinate":8.283365},{"xCoordinate":12.5,"yCoordinate":8.534275},{"xCoordinate":13.5,"yCoordinate":8.762649},{"xCoordinate":14.5,"yCoordinate":8.971407},{"xCoordinate":15.5,"yCoordinate":9.16318},{"xCoordinate":16.5,"yCoordinate":9.340328},{"xCoordinate":17.5,"yCoordinate":9.504964},{"xCoordinate":18.5,"yCoordinate":9.658975},{"xCoordinate":19.5,"yCoordinate":9.804039},{"xCoordinate":20.5,"yCoordinate":9.941645},{"xCoordinate":21.5,"yCoordinate":10.073105},{"xCoordinate":22.5,"yCoordinate":10.199575},{"xCoordinate":23.5,"yCoordinate":10.322062},{"xCoordinate":24.5,"yCoordinate":10.441442},{"xCoordinate":25.5,"yCoordinate":10.558473},{"xCoordinate":26.5,"yCoordinate":10.673803},{"xCoordinate":27.5,"yCoordinate":10.787982},{"xCoordinate":28.5,"yCoordinate":10.901473},{"xCoordinate":29.5,"yCoordinate":11.014664},{"xCoordinate":30.5,"yCoordinate":11.12787},{"xCoordinate":31.5,"yCoordinate":11.241348},{"xCoordinate":32.5,"yCoordinate":11.355298},{"xCoordinate":33.5,"yCoordinate":11.46988},{"xCoordinate":34.5,"yCoordinate":11.58521},{"xCoordinate":35.5,"yCoordinate":11.701371},{"xCoordinate":36.0,"yCoordinate":11.759784}],[{"xCoordinate":0.0,"yCoordinate":2.526904},{"xCoordinate":0.5,"yCoordinate":2.964656},{"xCoordinate":1.5,"yCoordinate":3.774849},{"xCoordinate":2.5,"yCoordinate":4.503255},{"xCoordinate":3.5,"yCoordinate":5.157412},{"xCoordinate":4.5,"yCoordinate":5.744752},{"xCoordinate":5.5,"yCoordinate":6.272175},{"xCoordinate":6.5,"yCoordinate":6.745993},{"xCoordinate":7.5,"yCoordinate":7.171952},{"xCoordinate":8.5,"yCoordinate":7.555287},{"xCoordinate":9.5,"yCoordinate":7.900755},{"xCoordinate":10.5,"yCoordinate":8.212684},{"xCoordinate":11.5,"yCoordinate":8.495},{"xCoordinate":12.5,"yCoordinate":8.751264},{"xCoordinate":13.5,"yCoordinate":8.984701},{"xCoordinate":14.5,"yCoordinate":9.198222},{"xCoordinate":15.5,"yCoordinate":9.394454},{"xCoordinate":16.5,"yCoordinate":9.575757},{"xCoordinate":17.5,"yCoordinate":9.744251},{"xCoordinate":18.5,"yCoordinate":9.90183},{"xCoordinate":19.5,"yCoordinate":10.050187},{"xCoordinate":20.5,"yCoordinate":10.190823},{"xCoordinate":21.5,"yCoordinate":10.32507},{"xCoordinate":22.5,"yCoordinate":10.454102},{"xCoordinate":23.5,"yCoordinate":10.578949},{"xCoordinate":24.5,"yCoordinate":10.700513},{"xCoordinate":25.5,"yCoordinate":10.819575},{"xCoordinate":26.5,"yCoordinate":10.936812},{"xCoordinate":27.5,"yCoordinate":11.052801},{"xCoordinate":28.5,"yCoordinate":11.168034},{"xCoordinate":29.5,"yCoordinate":11.282926},{"xCoordinate":30.5,"yCoordinate":11.39782},{"xCoordinate":31.5,"yCoordinate":11.513},{"xCoordinate":32.5,"yCoordinate":11.628692},{"xCoordinate":33.5,"yCoordinate":11.745079},{"xCoordinate":34.5,"yCoordinate":11.8623},{"xCoordinate":35.5,"yCoordinate":11.980456},{"xCoordinate":36.0,"yCoordinate":12.03991}],[{"xCoordinate":0.0,"yCoordinate":2.773802},{"xCoordinate":0.5,"yCoordinate":3.20951},{"xCoordinate":1.5,"yCoordinate":4.020561},{"xCoordinate":2.5,"yCoordinate":4.754479},{"xCoordinate":3.5,"yCoordinate":5.416803},{"xCoordinate":4.5,"yCoordinate":6.013716},{"xCoordinate":5.5,"yCoordinate":6.551379},{"xCoordinate":6.5,"yCoordinate":7.035656},{"xCoordinate":7.5,"yCoordinate":7.472021},{"xCoordinate":8.5,"yCoordinate":7.865533},{"xCoordinate":9.5,"yCoordinate":8.220839},{"xCoordinate":10.5,"yCoordinate":8.542195},{"xCoordinate":11.5,"yCoordinate":8.833486},{"xCoordinate":12.5,"yCoordinate":9.098246},{"xCoordinate":13.5,"yCoordinate":9.339688},{"xCoordinate":14.5,"yCoordinate":9.560722},{"xCoordinate":15.5,"yCoordinate":9.763982},{"xCoordinate":16.5,"yCoordinate":9.95184},{"xCoordinate":17.5,"yCoordinate":10.126434},{"xCoordinate":18.5,"yCoordinate":10.28968},{"xCoordinate":19.5,"yCoordinate":10.443295},{"xCoordinate":20.5,"yCoordinate":10.588812},{"xCoordinate":21.5,"yCoordinate":10.727592},{"xCoordinate":22.5,"yCoordinate":10.860844},{"xCoordinate":23.5,"yCoordinate":10.989635},{"xCoordinate":24.5,"yCoordinate":11.114904},{"xCoordinate":25.5,"yCoordinate":11.237473},{"xCoordinate":26.5,"yCoordinate":11.358059},{"xCoordinate":27.5,"yCoordinate":11.47728},{"xCoordinate":28.5,"yCoordinate":11.59567},{"xCoordinate":29.5,"yCoordinate":11.713683},{"xCoordinate":30.5,"yCoordinate":11.831705},{"xCoordinate":31.5,"yCoordinate":11.950054},{"xCoordinate":32.5,"yCoordinate":12.068997},{"xCoordinate":33.5,"yCoordinate":12.188748},{"xCoordinate":34.5,"yCoordinate":12.309477},{"xCoordinate":35.5,"yCoordinate":12.431315},{"xCoordinate":36.0,"yCoordinate":12.492682}],[{"xCoordinate":0.0,"yCoordinate":3.150611},{"xCoordinate":0.5,"yCoordinate":3.597396},{"xCoordinate":1.5,"yCoordinate":4.428873},{"xCoordinate":2.5,"yCoordinate":5.183378},{"xCoordinate":3.5,"yCoordinate":5.866806},{"xCoordinate":4.5,"yCoordinate":6.484969},{"xCoordinate":5.5,"yCoordinate":7.043627},{"xCoordinate":6.5,"yCoordinate":7.548346},{"xCoordinate":7.5,"yCoordinate":8.004399},{"xCoordinate":8.5,"yCoordinate":8.416719},{"xCoordinate":9.5,"yCoordinate":8.789882},{"xCoordinate":10.5,"yCoordinate":9.12811},{"xCoordinate":11.5,"yCoordinate":9.435279},{"xCoordinate":12.5,"yCoordinate":9.714942},{"xCoordinate":13.5,"yCoordinate":9.970338},{"xCoordinate":14.5,"yCoordinate":10.204418},{"xCoordinate":15.5,"yCoordinate":10.419863},{"xCoordinate":16.5,"yCoordinate":10.619101},{"xCoordinate":17.5,"yCoordinate":10.804329},{"xCoordinate":18.5,"yCoordinate":10.977527},{"xCoordinate":19.5,"yCoordinate":11.140475},{"xCoordinate":20.5,"yCoordinate":11.294771},{"xCoordinate":21.5,"yCoordinate":11.441847},{"xCoordinate":22.5,"yCoordinate":11.582978},{"xCoordinate":23.5,"yCoordinate":11.719299},{"xCoordinate":24.5,"yCoordinate":11.851817},{"xCoordinate":25.5,"yCoordinate":11.981419},{"xCoordinate":26.5,"yCoordinate":12.108888},{"xCoordinate":27.5,"yCoordinate":12.234907},{"xCoordinate":28.5,"yCoordinate":12.360072},{"xCoordinate":29.5,"yCoordinate":12.484898},{"xCoordinate":30.5,"yCoordinate":12.609827},{"xCoordinate":31.5,"yCoordinate":12.735234},{"xCoordinate":32.5,"yCoordinate":12.861438},{"xCoordinate":33.5,"yCoordinate":12.988699},{"xCoordinate":34.5,"yCoordinate":13.117232},{"xCoordinate":35.5,"yCoordinate":13.247207},{"xCoordinate":36.0,"yCoordinate":13.312776}],[{"xCoordinate":0.0,"yCoordinate":3.530203},{"xCoordinate":0.5,"yCoordinate":4.003106},{"xCoordinate":1.5,"yCoordinate":4.879525},{"xCoordinate":2.5,"yCoordinate":5.672889},{"xCoordinate":3.5,"yCoordinate":6.391392},{"xCoordinate":4.5,"yCoordinate":7.041836},{"xCoordinate":5.5,"yCoordinate":7.630425},{"xCoordinate":6.5,"yCoordinate":8.162951},{"xCoordinate":7.5,"yCoordinate":8.644832},{"xCoordinate":8.5,"yCoordinate":9.08112},{"xCoordinate":9.5,"yCoordinate":9.4765},{"xCoordinate":10.5,"yCoordinate":9.835308},{"xCoordinate":11.5,"yCoordinate":10.161536},{"xCoordinate":12.5,"yCoordinate":10.458854},{"xCoordinate":13.5,"yCoordinate":10.730626},{"xCoordinate":14.5,"yCoordinate":10.979925},{"xCoordinate":15.5,"yCoordinate":11.209555},{"xCoordinate":16.5,"yCoordinate":11.422068},{"xCoordinate":17.5,"yCoordinate":11.619777},{"xCoordinate":18.5,"yCoordinate":11.804779},{"xCoordinate":19.5,"yCoordinate":11.978966},{"xCoordinate":20.5,"yCoordinate":12.144043},{"xCoordinate":21.5,"yCoordinate":12.301541},{"xCoordinate":22.5,"yCoordinate":12.45283},{"xCoordinate":23.5,"yCoordinate":12.599135},{"xCoordinate":24.5,"yCoordinate":12.741544},{"xCoordinate":25.5,"yCoordinate":12.881023},{"xCoordinate":26.5,"yCoordinate":13.018424},{"xCoordinate":27.5,"yCoordinate":13.154497},{"xCoordinate":28.5,"yCoordinate":13.289897},{"xCoordinate":29.5,"yCoordinate":13.425194},{"xCoordinate":30.5,"yCoordinate":13.560881},{"xCoordinate":31.5,"yCoordinate":13.697379},{"xCoordinate":32.5,"yCoordinate":13.835046},{"xCoordinate":33.5,"yCoordinate":13.974182},{"xCoordinate":34.5,"yCoordinate":14.115032},{"xCoordinate":35.5,"yCoordinate":14.257796},{"xCoordinate":36.0,"yCoordinate":14.329944}],[{"xCoordinate":0.0,"yCoordinate":3.879077},{"xCoordinate":0.5,"yCoordinate":4.387423},{"xCoordinate":1.5,"yCoordinate":5.327328},{"xCoordinate":2.5,"yCoordinate":6.175598},{"xCoordinate":3.5,"yCoordinate":6.942217},{"xCoordinate":4.5,"yCoordinate":7.635323},{"xCoordinate":5.5,"yCoordinate":8.262033},{"xCoordinate":6.5,"yCoordinate":8.828786},{"xCoordinate":7.5,"yCoordinate":9.34149},{"xCoordinate":8.5,"yCoordinate":9.805593},{"xCoordinate":9.5,"yCoordinate":10.226124},{"xCoordinate":10.5,"yCoordinate":10.607722},{"xCoordinate":11.5,"yCoordinate":10.95466},{"xCoordinate":12.5,"yCoordinate":11.270871},{"xCoordinate":13.5,"yCoordinate":11.559963},{"xCoordinate":14.5,"yCoordinate":11.825241},{"xCoordinate":15.5,"yCoordinate":12.069725},{"xCoordinate":16.5,"yCoordinate":12.29617},{"xCoordinate":17.5,"yCoordinate":12.50708},{"xCoordinate":18.5,"yCoordinate":12.704728},{"xCoordinate":19.5,"yCoordinate":12.891168},{"xCoordinate":20.5,"yCoordinate":13.068254},{"xCoordinate":21.5,"yCoordinate":13.237653},{"xCoordinate":22.5,"yCoordinate":13.400856},{"xCoordinate":23.5,"yCoordinate":13.559197},{"xCoordinate":24.5,"yCoordinate":13.71386},{"xCoordinate":25.5,"yCoordinate":13.865896},{"xCoordinate":26.5,"yCoordinate":14.01623},{"xCoordinate":27.5,"yCoordinate":14.165672},{"xCoordinate":28.5,"yCoordinate":14.314932},{"xCoordinate":29.5,"yCoordinate":14.464622},{"xCoordinate":30.5,"yCoordinate":14.61527},{"xCoordinate":31.5,"yCoordinate":14.767324},{"xCoordinate":32.5,"yCoordinate":14.921166},{"xCoordinate":33.5,"yCoordinate":15.077109},{"xCoordinate":34.5,"yCoordinate":15.235412},{"xCoordinate":35.5,"yCoordinate":15.396279},{"xCoordinate":36.0,"yCoordinate":15.477724}],[{"xCoordinate":0.0,"yCoordinate":4.172493},{"xCoordinate":0.5,"yCoordinate":4.718161},{"xCoordinate":1.5,"yCoordinate":5.728153},{"xCoordinate":2.5,"yCoordinate":6.638979},{"xCoordinate":3.5,"yCoordinate":7.460702},{"xCoordinate":4.5,"yCoordinate":8.202193},{"xCoordinate":5.5,"yCoordinate":8.871384},{"xCoordinate":6.5,"yCoordinate":9.475466},{"xCoordinate":7.5,"yCoordinate":10.021014},{"xCoordinate":8.5,"yCoordinate":10.514064},{"xCoordinate":9.5,"yCoordinate":10.960172},{"xCoordinate":10.5,"yCoordinate":11.36445},{"xCoordinate":11.5,"yCoordinate":11.731602},{"xCoordinate":12.5,"yCoordinate":12.065948},{"xCoordinate":13.5,"yCoordinate":12.371453},{"xCoordinate":14.5,"yCoordinate":12.651749},{"xCoordinate":15.5,"yCoordinate":12.910152},{"xCoordinate":16.5,"yCoordinate":13.149687},{"xCoordinate":17.5,"yCoordinate":13.373106},{"xCoordinate":18.5,"yCoordinate":13.582902},{"xCoordinate":19.5,"yCoordinate":13.781331},{"xCoordinate":20.5,"yCoordinate":13.970425},{"xCoordinate":21.5,"yCoordinate":14.15201},{"xCoordinate":22.5,"yCoordinate":14.327718},{"xCoordinate":23.5,"yCoordinate":14.499004},{"xCoordinate":24.5,"yCoordinate":14.667158},{"xCoordinate":25.5,"yCoordinate":14.833316},{"xCoordinate":26.5,"yCoordinate":14.998478},{"xCoordinate":27.5,"yCoordinate":15.163512},{"xCoordinate":28.5,"yCoordinate":15.329172},{"xCoordinate":29.5,"yCoordinate":15.496103},{"xCoordinate":30.5,"yCoordinate":15.664853},{"xCoordinate":31.5,"yCoordinate":15.835883},{"xCoordinate":32.5,"yCoordinate":16.009575},{"xCoordinate":33.5,"yCoordinate":16.186239},{"xCoordinate":34.5,"yCoordinate":16.366119},{"xCoordinate":35.5,"yCoordinate":16.549405},{"xCoordinate":36.0,"yCoordinate":16.642369}],[{"xCoordinate":0.0,"yCoordinate":4.340293},{"xCoordinate":0.5,"yCoordinate":4.91013},{"xCoordinate":1.5,"yCoordinate":5.967102},{"xCoordinate":2.5,"yCoordinate":6.921119},{"xCoordinate":3.5,"yCoordinate":7.781401},{"xCoordinate":4.5,"yCoordinate":8.556813},{"xCoordinate":5.5,"yCoordinate":9.255615},{"xCoordinate":6.5,"yCoordinate":9.885436},{"xCoordinate":7.5,"yCoordinate":10.453314},{"xCoordinate":8.5,"yCoordinate":10.965736},{"xCoordinate":9.5,"yCoordinate":11.428676},{"xCoordinate":10.5,"yCoordinate":11.847633},{"xCoordinate":11.5,"yCoordinate":12.227661},{"xCoordinate":12.5,"yCoordinate":12.573402},{"xCoordinate":13.5,"yCoordinate":12.889108},{"xCoordinate":14.5,"yCoordinate":13.17867},{"xCoordinate":15.5,"yCoordinate":13.44564},{"xCoordinate":16.5,"yCoordinate":13.693251},{"xCoordinate":17.5,"yCoordinate":13.924442},{"xCoordinate":18.5,"yCoordinate":14.141875},{"xCoordinate":19.5,"yCoordinate":14.347954},{"xCoordinate":20.5,"yCoordinate":14.544842},{"xCoordinate":21.5,"yCoordinate":14.734482},{"xCoordinate":22.5,"yCoordinate":14.918606},{"xCoordinate":23.5,"yCoordinate":15.098756},{"xCoordinate":24.5,"yCoordinate":15.276296},{"xCoordinate":25.5,"yCoordinate":15.452424},{"xCoordinate":26.5,"yCoordinate":15.628189},{"xCoordinate":27.5,"yCoordinate":15.8045},{"xCoordinate":28.5,"yCoordinate":15.982139},{"xCoordinate":29.5,"yCoordinate":16.161769},{"xCoordinate":30.5,"yCoordinate":16.34395},{"xCoordinate":31.5,"yCoordinate":16.529146},{"xCoordinate":32.5,"yCoordinate":16.71773},{"xCoordinate":33.5,"yCoordinate":16.910002},{"xCoordinate":34.5,"yCoordinate":17.106191},{"xCoordinate":35.5,"yCoordinate":17.306461},{"xCoordinate":36.0,"yCoordinate":17.408165}],[{"xCoordinate":0.0,"yCoordinate":4.446488},{"xCoordinate":0.5,"yCoordinate":5.032625},{"xCoordinate":1.5,"yCoordinate":6.121929},{"xCoordinate":2.5,"yCoordinate":7.10625},{"xCoordinate":3.5,"yCoordinate":7.993878},{"xCoordinate":4.5,"yCoordinate":8.793444},{"xCoordinate":5.5,"yCoordinate":9.513307},{"xCoordinate":6.5,"yCoordinate":10.16135},{"xCoordinate":7.5,"yCoordinate":10.744924},{"xCoordinate":8.5,"yCoordinate":11.270838},{"xCoordinate":9.5,"yCoordinate":11.745385},{"xCoordinate":10.5,"yCoordinate":12.174357},{"xCoordinate":11.5,"yCoordinate":12.563083},{"xCoordinate":12.5,"yCoordinate":12.91645},{"xCoordinate":13.5,"yCoordinate":13.238933},{"xCoordinate":14.5,"yCoordinate":13.534622},{"xCoordinate":15.5,"yCoordinate":13.807244},{"xCoordinate":16.5,"yCoordinate":14.060194},{"xCoordinate":17.5,"yCoordinate":14.296548},{"xCoordinate":18.5,"yCoordinate":14.519093},{"xCoordinate":19.5,"yCoordinate":14.730343},{"xCoordinate":20.5,"yCoordinate":14.932559},{"xCoordinate":21.5,"yCoordinate":15.127765},{"xCoordinate":22.5,"yCoordinate":15.31777},{"xCoordinate":23.5,"yCoordinate":15.504178},{"xCoordinate":24.5,"yCoordinate":15.688406},{"xCoordinate":25.5,"yCoordinate":15.871699},{"xCoordinate":26.5,"yCoordinate":16.055142},{"xCoordinate":27.5,"yCoordinate":16.239671},{"xCoordinate":28.5,"yCoordinate":16.42609},{"xCoordinate":29.5,"yCoordinate":16.615076},{"xCoordinate":30.5,"yCoordinate":16.807196},{"xCoordinate":31.5,"yCoordinate":17.002914},{"xCoordinate":32.5,"yCoordinate":17.202598},{"xCoordinate":33.5,"yCoordinate":17.406538},{"xCoordinate":34.5,"yCoordinate":17.614947},{"xCoordinate":35.5,"yCoordinate":17.827972},{"xCoordinate":36.0,"yCoordinate":17.936247}],[{"xCoordinate":27.01,"yCoordinate":60.0}]]}
The error
Parse error on line 1:
^
Expecting '{', '['
Edit:
I dont know if my javascript is wrong currently im using this:
The javascript
function drawGraph(graph){
var idPatient=$("#consultationPatientId").val();
var patientName = $("#consultationPatientName").val();
$.jqplot('divChart', [[[1, 2],[3,5.12],[5,13.1],[7,33.6],[9,85.9],[11,219.9]]]);
var url;
switch(graph){
case 1:
url = "./consultation/graph1";
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
}
$.ajax({
type:"POST",
url:url,
data: { 'patient':idPatient },
dataType:"json",
success:function(response){
console.log("algo");
console.log(response);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
}
The ajax method returns a error of syntax so, im unable to even read the response, an i wanted to asing it to a variable in the javascript.
so, reding the error a lot of times i think that maybe this might be the error, should'nt be {[{{ instead of {[[{ the oder?
{"aaData":[[{"xCoordinate":0.0,"y
The Jquery ajax error
SyntaxError: JSON.parse: unexpected character at line 4 column 1 of the JSON data

Related

Variable scope & Callback woes

This program is reading through the nested object searching for a specific key & values. Once this data is found it has to initiate callback to send back the data. The object looks like this:
{
"name": "joel",
"title": "CTO",
"edu": {
"school": "RMB",
"college": "GNK",
"pg": "CDAC",
"extract": "This is a large text ..."
}
}
Here as I come from synchronous programming background I am not able to understand when I have to initiate the callback and also ensure variables are in scope
function parseData(str, callback) {
function recursiveFunction(obj) {
var keysArray = Object.keys(obj);
for (var i = 0; i < keysArray.length; i++) {
var key = keysArray[i];
var value = obj[key];
if (value === Object(value)) {
recursiveFunction(value);
}
else {
if (key == 'title') {
var title = value;
}
if (key == 'extract') {
var extract = value.replace(/(\r\n|\n|\r)/gm," ");
callback(null, JSON.stringify({title: title, text: extract}));
}
}
}
}
recursiveFunction(str, callback(null, JSON.stringify({title: title, text: extract})));
};
when this code is executed we get following error
/parseData.js:29
recursiveFunction(str, callback(null, JSON.stringify({title: title, text: extract})));
^
ReferenceError: title is not defined
Okay. So you want a function that retrieves the first property named title and the first property named extract from a nested object, no matter how deeply nested these properties are.
"Extract a property value from an object" is basically is a task in its own right, we could write a function for it.
There are three cases to handle:
The argument is not an object - return undefined
The argument contains the key in question - return the associated value
Otherwise, recurse into the object and repeat steps 1 and 2 - return according result
It could look like this:
function pluck(obj, searchKey) {
var val;
if (!obj || typeof obj !== "object") return;
if (obj.hasOwnProperty(searchKey)) return obj[searchKey];
Object.keys(obj).forEach(function (key) {
if (val) return;
val = pluck(obj[key], searchKey);
});
return val;
}
Now we can call pluck() on any object and with any key and it will return to us the first value it finds anywhere in the object.
Now the rest of your task becomes very easy:
var obj = {
"name": "joel",
"title": "CTO",
"edu": {
"school": "RMB",
"college": "GNK",
"pg": "CDAC",
"extract": "This is a large text ..."
}
}
var data = {
title: pluck(obj, "title"),
text: pluck(obj, "extract")
};
This function that you 've posted above has nothing to do with async programming. I will respond in the context of the chunk of code that you 've posted. The error that you have is because you are calling the recursiveFunction(str, callback(null, JSON.stringify({title: title, text: extract}))); but the title variable is nowhere defined. I can see a definition of the title but it is in the the context of the recursiveFunction function. The variables that you define in there are not visible outside of the scope of that function and that's why you have this error.
You are trying to do something strange in this line:
recursiveFunction(str, callback(null, JSON.stringify({title: title, text: extract})));
This line will invoke the callback and will pass in the recursiveFunction the results of this function. I would expect to see something like that in this line:
recursiveFunction(str, callback);

TypeError: Cannot read property 'name' of undefined

When i type the username which is there in Table "fun" i am not facing any error but when i type wrong name that is the name which is not there in the table am facing error this is the error and code is attached below.
app.post('/Fu',function(req,res,next){
var username = req.body.uname;
var password = req.body.pwd;
con.query('SELECT name FROM fun WHERE name = "' + username +'" ',function(err, result,fields) {
var w = result[0].name;
if( username == w ){
console.log("login successful");
}
else{
console.log("login not successful");
}
}
});
res.send("success1");
});
can someone please help with the error.
This error is probably related to the fact that, when username is not present in the table, result will be set to and empty array []. That means that it has no elements at all, so result[0] is undefined.
Make sure to check for that before trying to get result[0].name.
Also, I would suggest you a few things:
1) Add error checking before anything else;
2) You do not need to check if the name is equal to the result. The query you wrote will only return entries that already match that requirement;
3) Send the responses inside the callback function, otherwise, the user will get the "success1" answer before the query has even finished executing.
Here follows the resulting code:
app.post('/Fu',function(req, res, next){
var username = req.body.uname;
var password = req.body.pwd;
con.query('SELECT name FROM fun WHERE name = "' + username +'"', function(err, result,fields) {
if (err) {
response.sendStatus(500);
return;
}
if (result.lenght > 0) {
res.send('Login was successful');
} else {
res.send('Login was not successful');
}
});
});
Your problem is here:
var w = result[0].name;
Because the result set at [0] doesn't exist (query came back with nothing). you are trying to get the name value from something that is undefined.
Do something like this:
var w
if (result[0]) {
w = result[0].name
} else {
//your logic to handle this scenario
}
That assumes that whatever db querying library you use will return an array, even if empty. If it returns undefined instead of an empty array, your if statement would need to look more like: if (result && result[0])

Unity3d Facebook Response Data parsing

I was able to solve this solution is in the coment
I have problem with parsing data that I get rom Facebook on appRequestCallback.
The request is send and that part is ok. But I need to pars the send data for the internal uses.
the code is this
private void appRequestCallback(FBResult result)
{
Util.Log("appRequestCallback");
if (result != null)
{
var responseObject = Json.Deserialize(result.Text) as Dictionary<string, object>;
object obj = 0;
string resp = (string)responseObject["request"];
Util.Log ("resp : " + resp);
if (responseObject.TryGetValue("cancelled", out obj))
{
Util.Log("Request cancelled");
}
else if (responseObject.TryGetValue("request", out obj))
{
responseObject.TryGetValue("to", out obj);
string[] s = (string[]) obj;
Util.Log ("s: " + s);
AddPopupMessage("Request Sent", ChallengeDisplayTime);
Util.Log("Request sent");
}
}
}
In the console I get this
appRequestCallback
UnityEngine.Debug:Log(Object)
resp: 870884436303337
UnityEngine.Debug:Log(Object)
And then the error
InvalidCastException: Cannot cast from source type to destination type.
MainMenu.appRequestCallback (.FBResult result) (at Assets/Resources/Scripts/MainMenu.cs:482)
Facebook.AsyncRequestDialogPost.CallbackWithErrorHandling (.FBResult result)
Facebook.AsyncRequestString+c__Iterator0.MoveNext ()
The problem is in parsing of to: part of json file and I am not sure why. I have tried to cast it into string, string[], List<>, Array, ArrayList. As I see the problem is that I am not using the good cast type for the to: but I can not figure out what the correct cast type is

Nancy OnError will not accept a Response object?

The Nancy documentation seems to say that Pipelines.OnError should return null - as opposed to BeforeResponse which allows both null and a Response object.
All the examples like this one and many code samples here on StackOverflow show a Response being returned in the OnError, just like in the BeforeRequest.
When I attempt to return an HTTPStatus string for the Pipelines.OnError, everything works OK!
But when I attempt to return a Response, I get a compiler error:
Operator '+=' cannot be applied to operands of type 'Nancy.ErrorPipeline' and 'lambda expression'
I'm emulating almost exactly the code in the Nancy example, except for the fact that mine is a TinyIocContainer while the example's is using a StructureMap container and a StructureMap derived bootstrapper
Here's my code:
const string errKey = "My proj error";
const string creationProblem = "Message creation (HTTP-POST)";
const string retrievalProblem = "Message retrieval (HTTP-GET)";
public void Initialize(IPipelines pipelines)
{
string jsonContentType = "application/json";
byte[] jsonFailedCreate = toJsonByteArray(creationProblem);
byte[] jsonFailedRetrieve = toJsonByteArray(retrievalProblem);
Response responseFailedCreate = new Response
{
StatusCode = HttpStatusCode.NotModified,
ContentType = jsonContentType,
Contents = (stream) =>
stream.Write(jsonFailedCreate, 0, jsonFailedCreate.Length)
};
Response responseFailedRetrieve = new Response
{
StatusCode = HttpStatusCode.NotFound,
ContentType = jsonContentType,
Contents = (stream) =>
stream.Write(jsonFailedRetrieve, 0, jsonFailedRetrieve.Length)
};
// POST - error in Create call
pipelines.OnError += (context, exception) =>
{
// POST - error during Create call
if (context.Request.Method == "POST")
return responsefailedCreate;
// GET - error during Retrieve call
else if (context.Request.Method == "GET")
return responseFailedRetrieve;
// All other cases - not supported
else
return HttpStatusCode.InternalServerError;
};
}
private byte[] toJsonByteArray(string plainString)
{
string jsonString = new JObject { { errKey, plainString } }.ToString();
byte[] result = Encoding.UTF8.GetBytes(jsonString);
return result;
}
I had the same problem and I found a nice approach to the problem: http://paulstovell.com/blog/consistent-error-handling-with-nancy.
you should override RequestStartup on the Bootstrapper, here my test code:
protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context)
{
pipelines.OnError.AddItemToEndOfPipeline((ctx, ex) =>
{
DefaultJsonSerializer serializer = new DefaultJsonSerializer();
Response error = new JsonResponse(ex.Message,serializer);
error.StatusCode = HttpStatusCode.InternalServerError;
return error;
});
base.RequestStartup(container, pipelines, context);
}

Calling a function inside a function - converting AS2 to AS3

I currently have some code from here (https://github.com/jmhnilbog/Nilbog-Lib-AS2/blob/master/mx/mx/remoting/NetServiceProxy.as) which converts a function into a function. This code is shown below:
private var _allowRes:Boolean= false;
function __resolve( methodName:String ):Function {
if( _allowRes ) {
var f = function() :Object {
// did the user give a default client when he created this NetServiceProxy?
if (this.client != null) {
// Yes. Let's create a responder object.
arguments.unshift(new NetServiceProxyResponder(this, methodName));
}
else {
if (typeof(arguments[0].onResult) != "function") {
mx.remoting.NetServices.trace("NetServices", "warning", 3, "There is no defaultResponder, and no responder was given in call to " + methodName);
arguments.unshift(new NetServiceProxyResponder(this, methodName));
}
}
if(typeof(this.serviceName) == "function")
this.serviceName = this.servicename;
arguments.unshift(this.serviceName + "." + methodName);
return( this.nc.call.apply(this.nc, arguments));
};
return f;
}
else {
return null;
}
}
Basically what the code is designed to do is return a new function (returned as f) which performs the correct server operates. However, if I try and use this syntax in AS3, I get the following two errors:
Error: Syntax error: expecting semicolon before colon.
Error: Syntax error: else is unexpected.
How would I go about doing this? I know this is someone else's code, but I am trying to get the old AS1/2 mx.remoting functionality working in AS3. Cheers.