1. #1
    Ext User
    Join Date
    Oct 2008
    Posts
    28
    Vote Rating
    0
    samwells is on a distinguished road

      0  

    Default 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

  2. #2
    Sencha User Shawn.in.Tokyo's Avatar
    Join Date
    Feb 2009
    Posts
    42
    Vote Rating
    0
    Shawn.in.Tokyo is on a distinguished road

      0  

    Default


    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

  3. #3
    Ext User
    Join Date
    Oct 2008
    Posts
    28
    Vote Rating
    0
    samwells is on a distinguished road

      0  

    Default


    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

  4. #4
    Sencha User Shawn.in.Tokyo's Avatar
    Join Date
    Feb 2009
    Posts
    42
    Vote Rating
    0
    Shawn.in.Tokyo is on a distinguished road

      0  

    Default


    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:

    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 read an array I'd do something like:

    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());
            }
    To get a String from an object containing an array I do:

    Code:
    int array_index=0; //or whatever
    dataHolder.get("some_object").isObject().get("array_in_some_obj").isArray().get(array_index).isString().stringValue()
    That's what I do with my json coming from my server anyway.

    Best,

    Shawn

  5. #5
    Sencha User Shawn.in.Tokyo's Avatar
    Join Date
    Feb 2009
    Posts
    42
    Vote Rating
    0
    Shawn.in.Tokyo is on a distinguished road

      0  

    Default


    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

  6. #6
    Ext User
    Join Date
    Oct 2008
    Posts
    28
    Vote Rating
    0
    samwells is on a distinguished road

      0  

    Default


    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

  7. #7
    Software Architect
    Join Date
    Sep 2007
    Posts
    13,971
    Vote Rating
    132
    sven is a glorious beacon of light sven is a glorious beacon of light sven is a glorious beacon of light sven is a glorious beacon of light sven is a glorious beacon of light sven is a glorious beacon of light

      0  

    Default


    Code:
    String json = "{"
          + "\"status\":\"success\"" + ","
          + "\"user\":\"admin\""
          + "}";
    
        JSONObject obj = (JSONObject) JSONParser.parse(json);
          System.out.println(obj.get("status"));

  8. #8
    Ext User
    Join Date
    Oct 2008
    Posts
    28
    Vote Rating
    0
    samwells is on a distinguished road

      0  

    Default


    Thanks I got that
    But i was just wondering why JsObject is not working
    Anyways thanks for the reply

    Sam

Thread Participants: 2

Turkiyenin en sevilen filmlerinin yer aldigi xnxx internet sitemiz olan ve porn sex tarzi bir site olan mobil porno izle sitemiz gercekten dillere destan bir durumda herkesin sevdigi bir site olarak tarihe gececege benziyor. Sitenin en belirgin ozelliklerinden birisi de Turkiyede gercekten kaliteli ve muntazam, duzenli porno izle siteleri olmamasidir. Bu yuzden iste. Ayrica en net goruntu kalitesine sahip adresinde yayinlanmaktadir. Mesela diğer sitelerimizden bahsedecek olursak, en iyi hd porno video arşivine sahip bir siteyiz. "The Best anal porn videos and slut anus, big asses movies set..." hd porno faketaxi