JqGrid doesn't display JSON data - json

I have written code in JSP and used JSON object for displaying data in JqGrid.
I know my JSON object has data (I've log it's content) but it's not displaying in JqGrid. When I copied the JSON data into JSON string, it is getting loaded and successfully displaying in Jqgrid.
This is my JSP code:
JSONObject responcedata = new JSONObject();
responcedata.put("total",totalrow);
responcedata.put("page",cpage);
responcedata.put("records",rcount);
Report obj = new Report();
responcedata = obj.ReportGrid(responcedatal);
System.out.println(responcedata);
This is the content of my JSON object:
{"total":"21″,"rows":[{"cell":["HS","H","10","5","G","9288"],"id":"1″},{"cell":["",null,null,null,"G","2099"],"id":"2″},{"cell":["HS","F","3","53","G","86578"],"id":"3″},{"cell":["HS","F","7","26","G","8268"],"id":"4″},{"cell":["HS","F","8","54","G","221"],"id":"5″},{"cell":["HS","F","5","77","G","1020"],"id":"6″},{"cell":["HS","H","14","14","G","73334"],"id":"7″},{"cell":["HS","C","21","1","G&B","1512"],"id":"8″},{"cell":["HS","F","2","105","G","4960"],"id":"9″},{"cell":["HS","F","4","21","G","86889"],"id":"10″}],"records":11}
I used this JSON string and JqGrid is displaying data. When I use JSON object, the variable responcedata (in the code above) it only shows loading but data is never shown.

function callMe() {
$.ajax({
type : "POST",
url : "action name",
data : {}
}).done(function(data) {
// dat has list there is 5 column (id,fname,lname,username,password)
var len = data.length;
var t="";
for (var i=0; i<len; ++i) {
var id = data[i].id;
var fname = data[i].fname;
var lname = data[i].lname;
var username = data[i].username;
var password = data[i].password;
t+="<tr><td>"+id+"</td><td>"+fname+"</td><td>"+lname+"</td><td>"+username+"</td><td>"+password+"</td></tr>"
}
print(t,data);
});
}
function print(t, data) {
document.getElementById("welcometext").innerHTML = t;
}
in html:
<table id="welcometext" border="1">
</table>

I have included another Jsp in the Jsp file which contains json data.
I removed that include line. Now it is working fine.

Related

Reading an External json data

My script is looking at json files created on the server and retrieved using
//this is the code that gets the JSON file
jQuery.get(link,data)
Alerting out 'data' shows the json text as valid:
{"menu":[{"link":"../chapter-1/index.html","title":"Chapter 1", "dl": "chapter-1"}
I try and parse the returned 'data' variable
obj = JSON.parse(data);
So that I can loop through it and do my additional tasks.
When I try and JSON.parse(data) it fails
//The loop
for (i = 0; i < obj.menu.length; i++) {
console.log(obj.menu[i].dl);
download_packet(obj.menu[i].dl, i);
}
When I add the json manually
obj = JSON.parse('{"menu":[{"link":"../chapter-1/index.html","title":"Chapter 1", "dl": "chapter-1"}]}');
It works.
I have tried to convert to string etc.
Anyone know what I am stuffing up? - There are no error messages in the console.
This is the whole snippet:
function download_items(link) {
jQuery.get(link, function(data) {
var obj = data;
var i;
for (i = 0; i < obj.menu.length; i++) {
console.log(obj.menu[i].dl);
}
});
}
#Malcolm Mclean - put me on the right track.I converted my object to string then parsed it and it worked. A bit inefficient but its working.
Thanks Mal!
The JSON had to be parsed correctly:
jQuery.get(link, function(data) {
var obj = String(json_object);
obj = JSON.parse(json_object);
}

Creating a zip file from a JSON object using adm-zip

I'm trying to create a .zip file from a JSON object in Node.js. I'm using adm-zip to do that however I'm unable to make it work with this code:
var admZip = require('adm-zip');
var zip = new admZip();
zip.addFile(Date.now() + '.json', new Buffer(JSON.stringify(jsonObject));
var willSendthis = zip.toBuffer();
fs.writeFileSync('./example.zip', willSendthis);
This code creates example.zip but I'm not able to extract it, I tried with a .zipextractor but also with this code:
var admZip = require('adm-zip');
var zip = new admZip("./example.zip");
var zipEntries = zip.getEntries(); // an array of ZipEntry records
zipEntries.forEach(function(zipEntry) {
console.log(zipEntry.data.toString('utf8'));
});
It returns Cannot read property 'toString' of undefined at the line with console.log.
I could use zip.writeZip() for this example but I'm sending the .zipfile to Amazon S3 thus I need to use the method .toBuffer() to do something like this after using adm-zip:
var params = {Key: 'example.zip', Body: zip.toBuffer()};
s3bucket.upload(params, function(err, data) {...});
I don't see what is wrong, am I using the package correctly?
Try use zipEntry.getData().toString('utf8') instead zipEntry.data.toString('utf8'):
var admZip = require('adm-zip');
var zip = new admZip("./example.zip");
var zipEntries = zip.getEntries(); // an array of ZipEntry records
zipEntries.forEach(function(zipEntry) {
console.log(zipEntry.getData().toString('utf8'));
});

how to display json data in my listview in intel xdk

i am developing hybrid application by using intel xdk and jquerymobile framework for UI. i am trying to get json data from url and display it into listview. i already got json data from url but i don't know how to display those data in listview
This is my json data
{
"nl_wu":[
{
"id":"42",
"year":"2015",
"month":"jan",
"title":"newsletter",
"file":"http://school.com/sample.pdf"
},
{
"id":"39",
"year":"2015",
"month":"jan",
"title":"imagetest",
"file":"http://school.com/sampleimage.jpg"
}
]
}
This is json retrieving function
function showsmsmessage(){
var i;
var out ="";
var json;
var arr ;
var xhr = new XMLHttpRequest();
xhr.open("GET", "URL", false);
xhr.onload = function(){
if(xhr.status == 200)
{
var json_string = xhr.responseText;
json = eval ("(" + json_string + ")");
var s = JSON.stringify(json);
arr = $.parseJSON(s);
for(i=0;i<arr.nl_wu.length;i++)
{
out = arr.nl_wu[i];
alert(out.title);
}
}
else if(xhr.status == 404)
{
intel.xdk.notification.alert("Web Service Doesn't Exist", "Error");
}
else
{
intel.xdk.notification.alert("Unknown error occured while connecting to server", "Error");
}
}
xhr.send();
}
this is my html code for displaying json data in listview
<body>
<div data-role="listview" id="result">
<ul data-role="listview" data-autodividers="false">
<li>Adele</li>
</ul>
</div>
</body>
i want to display title from json in my listview (dynamically). i am getting title from json and just display it in alert message but i need to display in my listview. In my html code i just create one listview with static item.
My Question:-
1) how to display json data(title) in my listview
2) if i click the data item, particular pdf file or image file should download by using file path from json and display it in our app (i have file path in my json)
could you please tell me how to display json data(title) in my listview dynamically
add this instead of alert(out.title);
$("#result ul").append('<li>'+out.title+'</li>');
Its very easy to implement. Suppose list is the object which you are getting from the server. Now we just need to process the object for each array item.
use this code..
var html = '' ;
$.each(list.nl_wu, function(index, item){
html = "<li>" ;
html += "<a href='"+item.file+"'>"+item.title+"</a>" ;
html += "</li>" ;
}) ;
$("#result ul").html(html) ;
P.S. : There may be some comma mistake or something like that but i hope you got the logic behind this.

How to create an object of specific type from JSON in Parse

I have a Cloud Code script that pulls some JSON from a service. That JSON includes an array of objects. I want to save those to Parse, but using a specific Parse class. How can I do it?
Here's my code.
Parse.Cloud.httpRequest({
url: 'http://myservicehost.com',
headers: {
'Authorization': 'XXX'
},
success: function(httpResponse) {
console.log("Success!");
var json = JSON.parse(httpResponse.text);
var recipes = json.results;
for(int i=0; i<recipes.length; i++) {
var Recipe = Parse.Object.extend("Recipe");
var recipeFromJSON = recipes[i];
// how do i save recipeFromJSON into Recipe without setting all the fields one by one?
}
}
});
I think I got it working. You need to set the className property in the JSON data object to your class name. (Found it in the source code) But I did only try this on the client side though.
for(int i=0; i<recipes.length; i++) {
var recipeFromJSON = recipes[i];
recipeFromJSON.className = "Recipe";
var recipeParseObject = Parse.Object.fromJSON(recipeFromJSON);
// do stuff with recipeParseObject
}
Example from this page https://parse.com/docs/js/guide
var GameScore = Parse.Object.extend("GameScore");
var gameScore = new GameScore();
gameScore.save({
score: 1337,
playerName: "Sean Plott",
cheatMode: false
}, {
success: function(gameScore) {
// The object was saved successfully.
},
error: function(gameScore, error) {
// The save failed.
// error is a Parse.Error with an error code and message.
}
});
IHMO this question is not a duplicate of How to use Parse.Object fromJSON? [duplicate]
In this question the JSON has not been generated by the Parse.Object.toJSON function itself, but comes from another service.
const object = new Parse.Object('MyClass')
const asJson = object.toJSON();
// asJson.className = 'MyClass';
Parse.Object.fromJSON(asJson);
// Without L3 this results into:
// Error: Cannot create an object without a className
// It makes no sense (to me) why the Parse.Object.toJSON is not reversible

Binding JSON string to ListView in Metro apps?

I have a metro application(HTML5 & WinJS) in which am trying to display service data . Actually here am retrieving JSON data from my service but am unable to bind this data into listview . Anyone give me some working example.
Thank you.
You can use the WinJS.xhr() for this. You can read more about it on this link https://msdn.microsoft.com/pt-br/library/windows/apps/br229787.aspx and here is an example:
var path = "data/file.json";
function getData(path) {
WinJS.xhr({ url: path }).then(
function (response) {
var json = JSON.parse(response.responseText);
// Since this is an asynchronous function, you can't
// return the data, so you can:
// 1) retrieve the data to a namespace once the app loads.
var list = new WinJS.Binding.List(json);
Somenomespace.data = list;
// 2) or do all the binding inside the function.
var listView = document.getElementById("listViewID");
listView.winControl.itemDataSource = list.dataSource;
});
}
If you use the built in JSON.parse(jsonString) function you can loop through the content using a normal for loop as it then is a normal object and add it as usuall. Just remember to process or render the data.
Her is an example from code i had in a search page using listview:
var response = JSON.parse(data) ;
var originalResults = new WinJS.Binding.List();
for (x in response) {
originalResults.push(response[x]);
}
this.populateFilterBar(element, originalResults);
this.applyFilter(this.filters[0], originalResults);