PDA

View Full Version : correct jaon format??



kirankumar1231
21 Mar 2011, 1:45 AM
{"employee":[{"empid":123,"empname":"kiran kumar"},{"empid":1231,"empname":"kiran"}]}

is this a correct json format??
this values are not getting populated to my extjs grid??

this is the code

<%@

page import="java.io.PrintWriter"%>

<%@


page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>
<%@


taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>

<!


DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<


html>

<


head>

<


meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<


title>Insert title here</title>

<


link rel="stylesheet" type="text/css" href="css/ext-all.css">

<script type="text/javascript" src="js/ext-base.js"></script>
<script type="text/javascript" src="js/ext-all.js"></script>
<


script type="text/javascript">

Ext.onReady(function() {
Ext.QuickTips.init();
var reader=new Ext.data.JsonReader({
},[
{name: 'empid'},
{name: 'empname'}
]
);
var store=new Ext.data.Store({
root:"employee",
id: "empid",
autoLoad:true,
proxy : new Ext.data.HttpProxy({
url: 'http://localhost:8080/JsonExample/get.json'
}),
reader:reader
});
store.load();
alert(store.load());
var colModel = new Ext.grid.ColumnModel([
{header: "empid", width: 120, sortable: true, dataIndex: "empid"},
{header: "empname", width: 90, sortable: true, dataIndex: "empname"}
]);


var grid = new Ext.grid.GridPanel({ height:200, width:600, ds:store, cm: colModel});
grid.render(document.body);
grid.getSelectionModel().selectFirstRow();


});
</


script>

</


head>

<


body>

</


body>
</html>

this is my java code

import java.util.ArrayList;
import java.io.*;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import sun.net.www.http.HttpClient;
import net.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class Json extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// TODO Auto-generated method stub
ArrayList<Employee> emp=new ArrayList<Employee>();
Myservice serve=new Myservice();
emp=serve.getemployeesservice();
Iterator<Employee> empitr=emp.iterator();
JSONArray json=new JSONArray();
JSONObject JSONobj=new JSONObject();
while(empitr.hasNext()){
JSONObject jobj=new JSONObject();
Employee empl=new Employee();
empl=empitr.next();
jobj.put("empid",empl.getEmpid());
jobj.put("empname",empl.getEmpname());
json.add(jobj);
}
JSONobj.put("employee",json.toString());
System.out.println(JSONobj.toString());
PrintWriter pw=response.getWriter();
JSONobj.write(pw);
return mapping.findForward("success");
}

}


is der any prob with my java code??or with my extjs code
help me pls

fay
21 Mar 2011, 4:17 AM
That JSON's fine. I tested it as follows and it works as expected:

test.php



<?php
$buff = '{"employee":[{"empid":123,"empname":"kiran kumar"},{"empid":1231,"empname":"kiran"}]}';
header('Content-Type: application/json');
echo $buff;


test.html



<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Test</title>
<link rel="stylesheet" type="text/css" href="../../resources/css/ext-all.css"/>
<script type="text/javascript" src="../../adapter/ext/ext-base.js"></script>
<script type="text/javascript" src="../../ext-all-debug.js"></script>

<script>
Ext.onReady(function(){
var store = new Ext.data.JsonStore({
url: 'test.php',
root: 'employee',
idProperty: 'empid',
autoLoad: true,
fields: [
{name:'empid', type: 'int'},
{name:'empname', type: 'string'}
]
});

var colModel = new Ext.grid.ColumnModel([
{header: "empid", width: 120, sortable: true, dataIndex: "empid"},
{header: "empname", width: 90, sortable: true, dataIndex: "empname"}
]);

var grid = new Ext.grid.GridPanel({
height: 200,
width: 600,
store: store,
colModel: colModel,
renderTo: Ext.getBody()
});
});
</script>
</head>

<body>
</body>
</html>


There's obviously something either amiss with your server's setup or with your Java. I've no experience with Java so can't help you there. What are you seeing returned from the server with Firebug?