Tecnologia do Blogger.
RSS

Re: [androidbrasil-dev] cannot be converted to JSONArray

Você pode fazer algo como isso:

JSONObject jsonResult = new JSONObject("ResultDoWebService");
JSONArray jsonArray = jsonResult.getJSONArray("retorno");


Em 3 de julho de 2013 18:56, Vinny Valente <irmaocaradepau@gmail.com> escreveu:
Galera,

To com um erro na hora de converter o retorno do webservice para um array JSON.
Ja tentei de varias formas e a unica forma q funcionou é essa gambiarra... 
String teste = "[" + object.toString() +"]";
JSONArray message = (JSONArray)new JSONTokener(teste).nextValue();

Não sei se tem outra forma, mas todas que eu achei na net com exemplos e tudo mais no PHP são feitos assim e acabam me 
retornando sem os Colchetes... 
Alguem ai sabe outra forma ou onde esta meu erro?



WebService PHP
<?php
$arrayRetorno = array("nome" => "Vinny", "versao"=>"1.0", "teste"=>"webservice");
$json["retorno"] = $arrayRetorno;
echo json_encode($json));
?> 

Saida na Web
  {"retorno":{"nome":"Vinny","versao":"1.0","teste":"webservice"}} 

JAVA
JSONObject object = (JSONObject) new JSONTokener(resultado).nextValue();

Linha do erro   
JSONArray message = object.getJSONArray("retorno");

Exception
org.json.JSONException: Value {"teste":"webservice","nome":"Vinny","versao":"1.0"} at retorno of type org.json.JSONObject cannot be converted to JSONArray


--
You received this message because you are subscribed to the Google Groups "Android Brasil - Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to androidbrasil-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Diego Lopes Marques



--
You received this message because you are subscribed to the Google Groups "Android Brasil - Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to androidbrasil-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

0 comentários:

Postar um comentário