Extract JSON array data on Android received by DataSnap

0

Good afternoon. I would like to know how I can extract the data received by the server using Delphi's DataSnap. I'm doing this, but it's returning an error:

lista_versoes = dsProxy.getVersoes();

TJSONObject json_versao = new TJSONObject();
      for (int i=0; i < lista_versoes.size(); i++){
           json_versao = lista_versoes.getAsJsonObject(i);
           String versao = json_versao.getString("FVersao");
       }

The variable lista_versoes receives this value from dsProxy.getVersoes ():

["{\"type\":\"ClassesProjeto.TVersao\",\"id\":1,\"fields\":{\"FVersao\":1,\"FData_inc\":42230,\"FHora_inc\":0.904861111111111}}"]

Error:

  

java.lang.ClassCastException: com.embarcadero.javaandroid.TJSONString can not be cast to com.embarcadero.javaandroid.TJSONObject               at com.embarcadero.javaandroid.TJSONArray.getJSONObject (TJSONArray.java:220)               at com.nutricionista.tela.TelaLogin $ 4.run (TelaLogin.java:205)

    
asked by anonymous 15.03.2015 / 16:44

2 answers

1

No need to use gson no. If you prefer you can use this:

TJSONObject objJSON = (TJSONObject) mrUsuario.Get(IdUsuario);

TJSONObject objFIELDS = objJSON.getJSONObject("fields");
int varinteger = objFIELDS.getInt("FID");
String aux = objFIELDS.getString("FNome");
//FNome = é conforme vem no teu JSON no caso ai eu fiz um Get no meu modulo remoto q me retorno meu objeto TUsuario.
    
01.07.2015 / 23:04
1

Maybe you can use the Google GSON library ( link ) and turn the String into a Java object , for example:

String strJson = "{\"name\":\"Fulano\"}";
Gson gson = new Gson();
Person person = gson.fromJson(strJson, Person.class);
System.out.println(person.getName());

In this case, a Java Person class was created with the following code:

public class Person {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
    
19.03.2015 / 23:31