Skip to content Skip to sidebar Skip to footer

Java String Conversion To Json Can Not Be Done

I have JSON which I get from server: '[{\'id\':\'1\',\'name\':\'Milos\',\'city\':\'Smederevo\',\'email\':\'milos\',\'password\':\'\'}, {\'id\':\'3\',\'name\':\'Boban\',\'city\':\

Solution 1:

Try this solution.

s.replaceAll("\\\\", "");

This will definitely work.

Solution 2:

Problem has been solved with:

1) s = s.trim();
 2) s = s.substring(1, s.length()-1);
 3) s = s.replace("\\", "");

My json has been retrieved with "double quotes" on the beginning and on the end. I do not know how string variable can not figure out that "double quotes" is for "beginning" and for "ending" of string.

Thank you everybody for helping.

Solution 3:

It is working for me...

In your json the value of "Your Json" is inclused inside "" so it's considered a string not an array..

So the solution is one of two things:

If you can modify the server response, remove the "" from arround the json array. or parse it first as string and then create a json array from that string like..

String notes = jobj.getString("GetNotesResult");
jarray = newJSONArray(notes);

Solution 4:

I have no idea why its not working for you. Dot net web services do respond with \ but Java capable of parsing it. I did as below and it worked.

I've coded like this.

JSONArray users = null;
String jsStr ="[{\"id\":\"1\",\"name\":\"Milos\",\"city\":\"Smederevo\",\"email\":\"milos\",\"password\":\"\"},{\"id\":\"3\",\"name\":\"Boban\",\"city\":\"Beograd\",\"email\":\"bole\",\"password\":\"\"},{\"id\":\"4\",\"name\":\"Pele\",\"city\":\"Brazil\",\"email\":\"pele@pele.com\",\"password\":\"\"}, {\"id\":\"5\",\"name\":\"admin\",\"city\":\"Smederevo\",\"email\":\"admin\",\"password\":\"\"}]";
try {
    users = new JSONArray(jsStr);
} catch (JSONException e) {
    e.printStackTrace();
}

Log.v("JSONStr", String.valueOf(users.length()));
for(int i =0; i<users.length(); i++){
    try {
        Log.v("Name", users.getJSONObject(i).getString("name"));
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

See the LogCat

03-1816:34:46.459: V/JSONStr(307): 403-1816:34:46.479: V/Name(307): Milos
03-1816:34:46.479: V/Name(307): Boban
03-1816:34:46.479: V/Name(307): Pele
03-1816:34:46.479: V/Name(307): admin

Solution 5:

Your json will be valid only if you remove the back slashes () in between. You could use something like: strJson = strJson.replaceAll("\\\\", ""); OR strJson = strJson.replace("\\", ""); to remove the slashes () in between your json String. Please note that replaceAll() method treats the first argument as a regex, so you must double escape the backslash but, the replace() method treats it as a literal string, so you only have to escape it once. Please have a look at the below example for better understanding: I have kept your json text in a file named json.txt in my hard-drive for demonstration. The contents in the file looks like this:

[{\"id\":\"1\",\"name\":\"Milos\",\"city\":\"Smederevo\",\"email\":\"milos\",\"password\":\"\"},
 {\"id\":\"3\",\"name\":\"Boban\",\"city\":\"Beograd\",\"email\":\"bole\",\"password\":\"\"},
 {\"id\":\"4\",\"name\":\"Pele\",\"city\":\"Brazil\",\"email\":\"pele@pele.com\",\"password\":\"\"},
 {\"id\":\"5\",\"name\":\"admin\",\"city\":\"Smederevo\",\"email\":\"admin\",\"password\":\"\"}]

Now the code for getting the json array:

package com.stackoverflow.com;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


publicclassJsonTest {

    publicstaticvoidmain(String[] args) {    

        try {
            FileReaderfileReader=newFileReader("C:/Users/sarath_sivan/Desktop/json.txt");
            BufferedReaderbr=newBufferedReader(fileReader); 
            StringBuilderstrJsonBuilder=newStringBuilder();
            String strLine; 

            while((strLine = br.readLine()) != null) { 
                strJsonBuilder.append(strLine);
            }

            StringstrJson= strJsonBuilder.toString();
            strJson = strJson.replaceAll("\\\\", ""); /*OR you can use strJson = strJson.replace("\\", "");*/JSONArrayjsonArray=newJSONArray(strJson);

            for (inti=0; i < jsonArray.length(); i++) {
                JSONObjectmenuObject= jsonArray.getJSONObject(i);
                System.out.println("id: " + menuObject.getInt("id"));
                System.out.println("name: " + menuObject.getString("name"));
                System.out.println("city: " + menuObject.getString("city"));
                System.out.println("email: " + menuObject.getString("email"));
                System.out.println("password: " + menuObject.getString("password"));
                System.out.println();
                // do something with your JSON
            }

            fileReader.close();

        } catch (JSONException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

Post a Comment for "Java String Conversion To Json Can Not Be Done"