Help I can not convert a string with JSON format to JSONArray on android

1

Hi, I'm new to android studio I'm trying to convert us string with json format into a JSONArray object in android studio but I have the following error:

  

W / System.err: org.json.JSONException: Value [{"cve_prod": "ASPM1
  "," description ":" ASP. JACTO PJH DE 20 LTS "," active ":"   "," umed ":" PZA   "," l1 ":" 956.34 "," l3 ":" 956.34 "," l2 ":" 921.69 "," l4 ":" 893.20 "}, {" cve_prod ":" ASPM2   "," description ":" ASP.JACTO PJ PLUS18LTS "," active ":"   "," umed ":" PZA   "," l1 ":" 874.35 "," l3 ":" 862.29 "," l2 ":" 844.20 "," l4 ":" 804.00 "}

This is my function in c # is a webapi that encodes a datatable to json with the newtonsoft library.

[HttpGet]
    [Route("GetProductsPrices")]
    [ScriptMethod(ResponseFormat=ResponseFormat.Json)]
    public String GetProductsPrices()
    {
        ProductsModel productsModel = new ProductsModel();

       string json = JsonConvert.SerializeObject(productsModel.GetDataTableProducts());
       return json;    
    }

the result of the request is looked at in the following way which according to my experience is a valid JSONArray

[{"cve_prod":"ASPM1 ","desc_prod":"ASP. JACTO PJH DE 20 LTS ","desc1":" ","uni_med":"PZA ","prec_prod":1062.60000000,"prec_prod3":1024.10000000,"porcenieps":0.00,"prec_prod2":1062.60000000,"prec_prod4":893.20000000,"porceniva":0.00},{"cve_prod":"ASPM2 ","desc_prod":"ASP.JACTO PJ PLUS18LTS ","desc1":" ","uni_med":"PZA ","prec_prod":971.50000000,"prec_prod3":938.00000000,"porcenieps":0.00,"prec_prod2":958.10000000,"prec_prod4":804.00000000,"porceniva":0.00}]

this is the request I make from android

private void makeRequest(){

    NetworkInfo network = this.conManager.getActiveNetworkInfo();
    if(network.isConnected()){

        String url = "http://192.168.0.6:8080/api/products/getproductsprices";
        RequestQueue queue = Volley.newRequestQueue(this);

        StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                            makeTable(response);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
               error.printStackTrace();
            }
        });

         queue.add(stringRequest);



    }else{
        new AlertDialog
                .Builder(MainActivity.this)
                .setMessage("NO ESTAS CONECTADO, ES NECESARIO TENER ACCESO A INTERNET.")
                .show();
    }
}


private void makeTable(String json){



    try{
        JSONArray array = new JSONArray(json);

    }catch(JSONException ex){
        ex.printStackTrace();
    }


}

someone can help me .. thanks

    
asked by Nov Esk Eduard 04.12.2018 в 19:07
source

1 answer

0

It's not really a valid JsonArray, the error indicates that you do not have the closing container that indicates a Json Array, you also have line breaks apparently in the answer:

  

Value [{"cve_prod": "ASPM1", "description": "ASP. JACTO PJH OF 20 LTS   "," active ":" "," umed ":" PZA   "," l1 ":" 956.34 "," l3 ":" 956.34 "," l2 ":" 921.69 "," l4 ":" 893.20 "}, {" cve_prod ":" ASPM2   "," description ":" ASP.JACTO PJ PLUS18LTS "," active ":" "," umed ":" PZA   "," l1 ":" 874.35 "," l3 ":" 862.29 "," l2 ":" 844.20 "," l4 ":" 804.00 "}

I suggest you first delete these characters before processing the Json content using a REGEX:

string respuesta = Regex.Replace(repuestaJSON, @"\t|\n|\r", "");
    
answered by 04.12.2018 / 22:39
source