Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating JSONObject from string in JAVA (org.json)

Tags:

java

json

android

I'm using the org.json and I have that JSON in a string:

{"hours":["1","2","3","4","5","6","7","8","9","10","11"]}

When I try to create a new JSONObject from that string, I'm getting the following error:

Value of type ..String cannot be converted to JSONObject.

What am I doing wrong?

EDIT: assuming str has the content:

JSONObject jObject = new JSONObject(str);

EDIT2: Here's the real string:

{"hours":["1","2","3","4","5","6","7","8","9","10","11"],"lessons":["\u05d2\u05d9\u05d0,\u05ea\u05dc\u05de,\u05e2\u05e8\u05d1,\u05e4\u05d9\u05e1,\u05d1\u05d9\u05d5,\n\u05d6\u05d9\u05d5,\u05d5\u05d9\u05d9,\u05dc\u05d5\u05d9,\u05e4\u05d1\u05dc,\u05e8\u05d9\u05d9,","\u05d2\u05d9\u05d0,\u05ea\u05dc\u05de,\u05e2\u05e8\u05d1,\u05e4\u05d9\u05e1,\u05d1\u05d9\u05d5,\n\u05d6\u05d9\u05d5,\u05d5\u05d9\u05d9,\u05dc\u05d5\u05d9,\u05e4\u05d1\u05dc,\u05e8\u05d9\u05d9,","\u05d7\u05e0\"\u05d2 \u05d1\u05e0\u05d9\u05dd,\u05d7\u05e0\"\u05d2 \u05d1\u05e0\u05d5\u05ea\n\u05d9\u05de\u05e4\u05d5\u05dc\u05e1\u05e7\u05d9 \u05dc,\u05e0\u05d0\u05d5\u05e8 \u05de\u05dc\u05d9","\u05e1\u05e4\u05e8\u05d5\u05ea\n\u05d6\u05d9\u05dc\u05d3\u05de\u05df \u05d0\u05d5\u05e8\u05dc\u05d9","\u05d0\u05e0\u05d2\u05dc\u05d9\u05ea\n\u05d1\u05e9\u05d9 \u05e9\u05d5\u05dc\u05de\u05d9\u05ea","\u05d0\u05e0\u05d2\u05dc\u05d9\u05ea\n\u05d1\u05e9\u05d9 \u05e9\u05d5\u05dc\u05de\u05d9\u05ea","\u05ea\u05e0\",\u05e2\u05e8\u05d1,\u05e6\u05e8\u05e4,\u05e7\u05d5\u05dc,\u05d1\u05d9\u05d5,\n\u05e9\u05d7\u05e3,\u05de\u05d6\u05dc,\u05dc\u05e1\u05e7,\u05d8\u05d5\u05e4,\u05dc\u05d5\u05d9,","\u05ea\u05e0\",\u05e2\u05e8\u05d1,\u05e6\u05e8\u05e4,\u05e7\u05d5\u05dc,\u05d1\u05d9\u05d5,\n\u05e9\u05d7\u05e3,\u05de\u05d6\u05dc,\u05dc\u05e1\u05e7,\u05d8\u05d5\u05e4,\u05dc\u05d5\u05d9,","\u05e2\u05e8\u05d1,\u05e7\u05d5\u05dc,\u05d1\u05d9\u05d5,\u05d8\u05db\",\u05e8\u05d5\u05e1,\n\u05dc\u05d5\u05d9,\u05e7\u05de\u05d7,\u05d1\u05e1\u05d5,\u05d5\u05e7\u05e1,\u05e6\u05d5\u05e8,","\u05e2\u05e8\u05d1\u05d9,\u05e7\u05d5\u05dc\u05e0,\u05d1\u05d9\u05d5\u05d8,\u05d8\u05db\"\u05dd\n\u05dc\u05d5\u05d9,\u05d1\u05e1\u05d5\u05df,\u05d5\u05e7\u05e1,\u05e6\u05d5\u05e8",""]}

EDIT3: Finally, here's my code, hopefully you can fix that problem.

StringBuilder str = new StringBuilder();
        TextView textView = new TextView(this);
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet("http://ohel-shem.com/~iApplication/appdata.php?pass=12345&request=cp&day=4%20&class=7&layer=12");
        Log.d("MyTag","Before");
        HttpResponse response = client.execute(request);

//       Get the response
        BufferedReader rd = new BufferedReader
          (new InputStreamReader(response.getEntity().getContent()));

        String line = "";
        while ((line = rd.readLine()) != null) {
          str.append(line);
        } 
        Log.d("MyTag","hello");
        String s = str.toString();
                s= s.replaceAll("\n", "\\n");
                JSONObject json = new JSONObject(s); //Occures an error converting string to jsonobject
like image 683
idish Avatar asked Sep 06 '12 22:09

idish


2 Answers

Welcome to the wonderful world of \n breaking your parser.

\n specifies a newline character, i'm not sure exactly why it breaks the parser but adding a \ to it will espace the control character.

@Test
public void test() throws JSONException {

        String s = 
        "{\"hours\":[\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\",\"10\",\"11\"],\"lessons\":[\"\u05d2\u05d9\u05d0,\u05ea\u05dc\u05de,\u05e2\u05e8\u05d1,\u05e4\u05d9\u05e1,\u05d1\u05d9\u05d5,\n\u05d6\u05d9\u05d5,\u05d5\u05d9\u05d9,\u05dc\u05d5\u05d9,\u05e4\u05d1\u05dc,\u05e8\u05d9\u05d9,\",\"\u05d2\u05d9\u05d0,\u05ea\u05dc\u05de,\u05e2\u05e8\u05d1,\u05e4\u05d9\u05e1,\u05d1\u05d9\u05d5,\n\u05d6\u05d9\u05d5,\u05d5\u05d9\u05d9,\u05dc\u05d5\u05d9,\u05e4\u05d1\u05dc,\u05e8\u05d9\u05d9,\",\"\u05d7\u05e0\\\"\u05d2 \u05d1\u05e0\u05d9\u05dd,\u05d7\u05e0\\\"\u05d2 \u05d1\u05e0\u05d5\u05ea\n\u05d9\u05de\u05e4\u05d5\u05dc\u05e1\u05e7\u05d9 \u05dc,\u05e0\u05d0\u05d5\u05e8 \u05de\u05dc\u05d9\",\"\u05e1\u05e4\u05e8\u05d5\u05ea\n\u05d6\u05d9\u05dc\u05d3\u05de\u05df \u05d0\u05d5\u05e8\u05dc\u05d9\",\"\u05d0\u05e0\u05d2\u05dc\u05d9\u05ea\n\u05d1\u05e9\u05d9 \u05e9\u05d5\u05dc\u05de\u05d9\u05ea\",\"\u05d0\u05e0\u05d2\u05dc\u05d9\u05ea\n\u05d1\u05e9\u05d9 \u05e9\u05d5\u05dc\u05de\u05d9\u05ea\",\"\u05ea\u05e0\\\",\u05e2\u05e8\u05d1,\u05e6\u05e8\u05e4,\u05e7\u05d5\u05dc,\u05d1\u05d9\u05d5,\n\u05e9\u05d7\u05e3,\u05de\u05d6\u05dc,\u05dc\u05e1\u05e7,\u05d8\u05d5\u05e4,\u05dc\u05d5\u05d9,\",\"\u05ea\u05e0\\\",\u05e2\u05e8\u05d1,\u05e6\u05e8\u05e4,\u05e7\u05d5\u05dc,\u05d1\u05d9\u05d5,\n\u05e9\u05d7\u05e3,\u05de\u05d6\u05dc,\u05dc\u05e1\u05e7,\u05d8\u05d5\u05e4,\u05dc\u05d5\u05d9,\",\"\u05e2\u05e8\u05d1,\u05e7\u05d5\u05dc,\u05d1\u05d9\u05d5,\u05d8\u05db\\\",\u05e8\u05d5\u05e1,\n\u05dc\u05d5\u05d9,\u05e7\u05de\u05d7,\u05d1\u05e1\u05d5,\u05d5\u05e7\u05e1,\u05e6\u05d5\u05e8,\",\"\u05e2\u05e8\u05d1\u05d9,\u05e7\u05d5\u05dc\u05e0,\u05d1\u05d9\u05d5\u05d8,\u05d8\u05db\\\"\u05dd\n\u05dc\u05d5\u05d9,\u05d1\u05e1\u05d5\u05df,\u05d5\u05e7\u05e1,\u05e6\u05d5\u05e8\",\"\"]}";
        s= s.replaceAll("\n", "\\n");
        JSONObject json = new JSONObject(s); 
    }

works fine.

Edit: looking at your code you are in luck. Some services sometimes add a crazy character to the start of the feed and it looks like this is what happened here. You just need to trim it from the string.

Example :

s = s.substring(s.indexOf("{")); 
like image 121
MikePatel Avatar answered Sep 23 '22 17:09

MikePatel


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

public class JSONTest {

    public static void main(String[] args) throws JSONException {
        String str = "{\"hours\":[\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\",\"10\",\"11\"]}";
        JSONObject jsonObject = new JSONObject(str);
        System.out.println(jsonObject);
    }
}

Output is : {"hours":["1","2","3","4","5","6","7","8","9","10","11"]}

like image 37
R H Avatar answered Sep 20 '22 17:09

R H