On Saturday, April 2, 2016 at 12:52:51 PM UTC+2, schmitt kevin wrote:
>
> Hello everyone , can you help me please?
>
> in my server wich connect with mysql databse i send to my client one json 
> table like that 
>
> rs = stmt.executeQuery("SELECT * FROM test");
> res ="[";
> while (rs.next()) {
> String titre = rs.getString("id");
> String auteur = rs.getString("comment");
> res += "\t{\"Titre\": \""+titre+ "\",\n";
> res += "\t\"Auteur\": \""+auteur+ "\",\n";
> res += "\t},\n";
> }
> res += "]\n";
>
>
So this returns an array of objects, each object having a Titre and Auteur 
property; e.g.
[    {"Titre": "titre 1",
    "Auteur": "auteur 1",
    },
    {"Titre": "titre 2",
    "Auteur": "auteur 2",
    },
]

Note however that you have dangling commas inside both objects and the 
array, and that's not valid JSON (neither http://json.org nor 
http://www.ecma-international.org/ecma-262/5.1/#sec-15.12.1) so it'll 
likely break at parsing time.

(side note: either use a proper JSON library, or properly escape the titre 
and auteur values or you'll risk corrupting your output)
 

>  and after i try to receive and use json data but dont work 
>
> in my client side 
>
> final ProjectServiceAsync getService = GWT.create(ProjectService.class);
> getService.connection( new AsyncCallback<String>() 
>   {
>    public void onFailure(Throwable caught) 
>    {
>     System.out.println("error");
>    }
>            
>    public void onSuccess(String result) 
>    {
>     JSONValue jsonValue = JSONParser.parseStrict(result);
>     com.google.gwt.json.client.JSONObject customerObject = 
> jsonValue.isObject();
>     com.google.gwt.json.client.JSONArray jsonArray = 
> customerObject.get("titre").isArray();
>     StringBuilder builder = new StringBuilder("** Livre ** \n"); 
>     
> builder.append(jsonArray.get(0).isObject().get("Titre").isString().stringValue()).append("
>  
> ");
>     Window.alert(builder.toString());
>    
>     //resultTxt.setText(result);
>    }
>   });
> }
>
>
This code expects an object at top-level with a Titre property being an 
array of strings; this is not what the sender produces. So even if you fix 
the dangling commas in the JSON produced by the server, this won't work.

Anyway, I suggest using overlay types and JsonUtils.safeEval instead of 
com.google.gwt.json; or even directly go with JsInterop.

final class Livre extends JavaScriptObject {
  protected Livre() {}
  public native String getTitre() /*-{ return this.Titre; }-*/;
  public native String getAuteur() /*-{ return this.Auteur; }-*/;
}
JsArray<Livre> livres = JsonUtils.safeEval(result);
for (int i = 0, l = livres.length(); i < l; i++) {
  builder.append(livres.get(i).getTitre()).append(" ");
}
 
(side note: similarly to the JSON generation on server-side, if you intend 
to use the 'builder' with toInnerHTML or similar, make sure you properly 
escape it or, better, use SafeHtml, so avoid XSS)

With JsInterop (jsinterop.annotations.*, you'd need 2.8.0-beta1 I believe), 
it'd be something like:
@JsType
interface Livre {
  @JsProperty(name="Titre") String getTitre();
  @JsProperty(name="Auteur") String getAuteur();
}
Livre[] livres = (Livre[]) JsonUtils.<Object>safeEval(result);

-- 
You received this message because you are subscribed to the Google Groups "GWT 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

Reply via email to