Convert Jsonarray to Object

Asked

Viewed 118 times

0

I’m having trouble converting an array I get via webservice

CustomRequest request = new CustomRequest(Request.Method.GET,
            "http://" + url + "/service/usuario/mural",
            null,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    ocultaProgressBar();
                    Gson gson = new Gson();
                    mural = new ArrayList<>();
                    for (int i = 0, tamI = response.length(); i < tamI; i++) {
                        try {
                            Configuracao configuracao = new Configuracao();
                            configuracao = gson.fromJson(response.getString(i), Configuracao.class);
                            mural.add(configuracao);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    iniciarFragmentoHome();
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                    Log.e("Erro",error.toString());
                    ocultaProgressBar();

                }
            });

in java I do the same above, already in Swift 3 that is the problem... I am doing so:

Alamofire.request(urlbusca, method: .get)
        .responseJSON { response in
            if(response.result.isSuccess){
                let jsonc = JSON(response.result.value!)
                print(jsonc)

                let resData = jsonc[0].arrayObject as! String
                print(resData)
                let conf = Configuracao(json:resData)
                self.mensagens.append(conf)

                print(self.mensagens)

                if self.mensagens.count>0{
                    print("ok")
                    self.tbInicio.reloadData()
                }
                else {
                    let alert = UIAlertController(title: "Alerta", message: "Sem conexão com a internet!", preferredStyle: .alert)
                    alert.addAction(UIAlertAction(title: "ok", style: .default) { action in
                        // perhaps use action.title here
                    })
                    self.present(alert, animated: true)
                }
            }


    }

I don’t know how to use the get to retrieve the object

  • What specifically is going wrong, Bruno? "Sponse" comes empty?

  • Oba! actually could not get the values... but I’ve decided to post the solution..

  • Felipe I have another problem, could you help me? https://answall.com/questions/192148/erro-evreflection-boll

1 answer

0

    Alamofire.request(urlbusca, method: .get)
        .responseJSON { response in
            if(response.result.isSuccess){
                let jsonc = JSON(response.result.value!)
                for config  in jsonc{
                    let confi = config.1.rawString()
                    let conf = Configuracao(json:confi)
                    self.mensagens.append(conf)


                }

                if self.mensagens.count>0{
                    let appDelegate = UIApplication.shared.delegate as! AppDelegate
                    appDelegate.mural = self.mensagens

                    self.tbInicio.reloadData()
                }
                else {
                    let alert = UIAlertController(title: "Alerta", message: "Sem conexão com a internet!", preferredStyle: .alert)
                    alert.addAction(UIAlertAction(title: "ok", style: .default) { action in
                        // perhaps use action.title here
                    })
                    self.present(alert, animated: true)
                }
            }


    }

Browser other questions tagged

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