How can I parse JSON with comma in android? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How can I parse JSON with comma in android? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

Im trying to parse JSON and my code is not working with one URL and it works with other. I think its the coma in question but I did not sure so please look at my code and help. I tried to replace coma with dot in loop, but what i get is JSON Exception. And when I change the URL, there is no exception. Confusing… This is not working URL: http://api.hnb.hr/tecajn/v1 And this is one I tested and it works: https://newsapi.org/v2/top-headlines?country=us&category=business&apiKey=e402c76fc8584a1c81849179f1277a74 While changed the URL I also changed the data, so its not the problem in writing..I guess

Here is my code for coma replacement:

 private String convertStreamToString(InputStream is) {
 BufferedReader reader = new BufferedReader(new 
InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line;
    try {
        while ((line = reader.readLine()) != null) {
            String coma = line.replaceFirst(",",".");
            sb.append(coma).append('n');
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return sb.toString();
}

And the other part with URL and JSONArray:

    @Override
    protected Void doInBackground(Void... arg0) {
        URLconnection urlConn = new URLconnection();
        // Making a request to url and getting response
        String url = "http://api.hnb.hr/tecajn/v1";
        //.........connection.........
        String response = urlConn.makeServiceCall(url);

        Log.e(TAG, "Response from url: " + response);
        if (response != null) {
            try {
                JSONObject jsonObj = new JSONObject(response);

                // Getting JSON Array node
                arr = jsonObj.getJSONArray("values");

                for (int i = 0; i < article.length(); i++) {

                    JSONObject c = arr.getJSONObject(i);
                    header = c.getString("Valuta");

               }

            } catch (final JSONException e) {
                Log.e(TAG, "Json parsing error.");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                                "Json parsing error: 216",
                                Toast.LENGTH_LONG).show();
                    }
                });
            }

Answer

Commas aren’t the issue. It’s the very first character. One is a square bracket and the other is a curly one.

You therefore need to parse the first response as an Array rather than an Object

new JSONArray(response);

That being said, you therefore cannot use the same methods for both URLs

We are here to answer your question about How can I parse JSON with comma in android? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji