Подтвердить что ты не робот

Создание JSONObject из строки в JAVA (org.json)

Я использую org.json, и у меня есть JSON в строке:

{ "Часы": [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"] }

Когда я пытаюсь создать новый JSONObject из этой строки, я получаю следующую ошибку:

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

Что я делаю не так?

EDIT: предполагается, что str имеет контент:

JSONObject jObject = new JSONObject(str);

EDIT2: Здесь настоящая строка:

{"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: Наконец, здесь мой код, надеюсь, вы можете исправить эту проблему.

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
4b9b3361

Ответ 1

Добро пожаловать в чудесный мир \n, разбивающий ваш парсер.

\n указывает символ новой строки, я точно не знаю, почему он разрывает синтаксический анализатор, но добавление\к нему будет задерживать символ управления.

@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); 
    }

работает отлично.

Изменение: глядя на ваш код, вам повезло. Некоторые службы иногда добавляют сумасшедшего персонажа в начало фида, и похоже, что это произошло здесь. Вам просто нужно обрезать его из строки.

Пример:

s = s.substring(s.indexOf("{")); 

Ответ 2

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);
    }
}

Выход: {"hours":["1","2","3","4","5","6","7","8","9","10","11"]}