Extract JSON array data on Android received by Datasnap

Asked

Viewed 731 times

0

Good afternoon. I would like to know how I can extract the data received by the server using Datasnap from Delphi. I’m doing it this way, 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 version_list variable receives this value from dsProxy.getVerses():

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

Error:

java.lang.Classcastexception: com.embarcadero.javaandroid.Tjsonstring cannot 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)

2 answers

1

Maybe you can use Google’s GSON library (https://code.google.com/p/google-gson/) and transform 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;
    }
}

1


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

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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.