-
9 Feb 2009 7:35 PM #1
How to create JSON object
How to create JSON object
How to create a Jsobject from a string
I used the below code but it give me error
String json = "{"
+ "\"status\":\"success\"" + ","
+ "\"user\":\"admin\""
+ "}";
JsObject obj = new JsObject(output);
System.out.println(obj.getString("status"));
ERROR] Uncaught exception escaped
com.google.gwt.core.client.JavaScriptException: (SyntaxError): Expected identifier, string or number
number: -2146827260
description: Expected identifier, string or number
at com.extjs.gxt.ui.client.js.JsUtil.eval(Native Method)
at com.extjs.gxt.ui.client.js.JsObject.<init>(JsObject.java:35)
Can some tell me how to create js object from json string
Its urgent
Thanks
Sam
-
9 Feb 2009 9:24 PM #2
Have you looked at:
http://www.gwtapps.com/doc/html/com....tml#JSONObject
I don't know that ExtGWT does that differently than GWT in this respect.
Shawn
-
9 Feb 2009 10:03 PM #3
Thanks for the reply
I already looked at that gwt JSONObject
but this doesn't have any api to create jsonobject using string ...
Actually wat i need is that , the json string will be coming from the server side
and i need to build json object using that string in client
JOSNObject doesn't allow to create jsonobject using string
Ext-Gwt JsObject has that api but its giving me error .
Can u suggest me how to create jsonobject using string ...
no matter if its gwt or ext gwt api
Thanks
Sam
-
10 Feb 2009 1:00 AM #4
You have a json string coming from the server?
Ok, simple as pie or actually easier.
Code:import com.google.gwt.core.client.JavaScriptObject; import com.google.gwt.json.client.JSONArray; import com.google.gwt.json.client.JSONObject; import com.google.gwt.json.client.JSONValue;
sample method:
To read an array I'd do something like:Code:JSONObject dataHolder; String recieved_status; public String handleData(JavaScriptObject jsData) { dataHolder = new JSONObject(jsData); recieved_status = dataHolder.get("status").isString().stringValue(); return recieved_status; } }
To get a String from an object containing an array I do:Code:JSONValue jval = dataHolder.get("some_array"); int count = jval.isArray().size(); String s[] = new String[count]; for (int i = 0; i < jval.isArray().size(); i++) { s[i]=(jval.isArray().get(i).isString().stringValue()); }
That's what I do with my json coming from my server anyway.Code:int array_index=0; //or whatever dataHolder.get("some_object").isObject().get("array_in_some_obj").isArray().get(array_index).isString().stringValue()
Best,
Shawn
-
10 Feb 2009 1:17 AM #5
By the way, I think the GXT api will work too.
The problem may be your JSON. I don't think it is an object but rather an array.
Have you looked at and tried
com.extjs.gxt.ui.client.js.JsArray ?
Best,
Shawn
-
10 Feb 2009 2:59 AM #6
Shawn thanks for ur reply
But still i m confused
I am making a server call like this
RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, url);
try {
builder.setHeader("Content-Type", "application/x-www-form-urlencoded");
// builder.setTimeoutMillis(2000);
builder.sendRequest(postData.toString(),
new RequestCallback() {
public void onError(Request request,
Throwable exception) {
}
public void onResponseReceived(Request request,
Response response) {
// This response string is a json format string which i build in server and send back
// for examples say like this
// "{" + "\"status\":\"success\"" + "," + "\"user\":\"admin\""
// + "}";
// How to i create a JSONobject using this string
processResponse(response.getText());
}
});
} catch (RequestException e) {
Window.alert("Failed to send the request: " + e.getMessage());
}
}
Your method handleData is fine but it need a JavaScriptObject jsData
so then how to create that JavaScriptObject using string
My processResponse method is like this which is giving error
void processResponse (String json) {
// This gives me error
JsObject obj = new JsObject(json);
System.out.println(obj.getString("status"));
System.out.println(obj.getString("user"));
}
Can u just tell me now how to implement your idea into this
I am getting confused in with ur code .
Sam
-
10 Feb 2009 3:05 AM #7
Code:String json = "{" + "\"status\":\"success\"" + "," + "\"user\":\"admin\"" + "}"; JSONObject obj = (JSONObject) JSONParser.parse(json); System.out.println(obj.get("status"));
-
10 Feb 2009 5:07 AM #8
Thanks I got that
But i was just wondering why JsObject is not working
Anyways thanks for the reply
Sam


Reply With Quote