Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android JSONObject文本必须以';{';在的字符1处_Android - Fatal编程技术网

Android JSONObject文本必须以';{';在的字符1处

Android JSONObject文本必须以';{';在的字符1处,android,Android,我有一个显示JSON数组的PHP API,然后我将其读入Android应用程序 我移动了服务器,安卓应用程序坏了 我认为这是身份验证,并认为我会重新构建Android应用程序(这是我的第一个应用程序,并认为重新编写可以让事情变得更好) 由于某种原因,我现在得到这个异常错误 我在某个地方读到,我需要在PHP JSON_编码中解析JSON_FORCE_对象 json_encode($arrMyData, JSON_FORCE_OBJECT); 但我运行的是PHP5.2(选项参数在PHP5.3中出现

我有一个显示JSON数组的PHP API,然后我将其读入Android应用程序

我移动了服务器,安卓应用程序坏了

我认为这是身份验证,并认为我会重新构建Android应用程序(这是我的第一个应用程序,并认为重新编写可以让事情变得更好)

由于某种原因,我现在得到这个异常错误

我在某个地方读到,我需要在PHP JSON_编码中解析JSON_FORCE_对象

json_encode($arrMyData, JSON_FORCE_OBJECT);
但我运行的是PHP5.2(选项参数在PHP5.3中出现)

我给你的密码

private void displayAllStories(){

    String line;
    int intNumStories       = 0;
    JSONObject arrAllStories;
    LinearLayout storiesLayout = (LinearLayout) findViewById(R.id.lyoutStoriesMain);
    storiesLayout.removeAllViewsInLayout();
    try {

        while((line = this.jsonResult.readLine()) != null){
            JSONObject arrStories;
            arrStories              = new JSONObject(line.trim());
            intNumStories           = Integer.parseInt(arrStories.optString("NumStories"));
            arrAllStories           = arrStories.getJSONObject("StoryData");

            this.strDebug += "We have "+intNumStories+"\n";
        }
    } catch (IOException e) {
        this.strDebug += "Error (3) "+e.getLocalizedMessage()+"\n";
    } catch (JSONException e) {

        this.strDebug += "Error (4) "+e.getLocalizedMessage()+"\n";
    }
}
以及来自网站的编码数据

{
   "NumStories":1,
   "StoryData":{
      "Story0":{
         "ID":"1020",
         "OWERNAME":"Alicia",
         "STORYMAIN":"Good evening my son was born with bilateral club feet. When he was a week old we started serial casting once a week for 3 months and then he was placed in braces for the next 6 months for a 23 hour period and then for the next 3 months just durning the night. This last visit the doctor said that he needs to have his tendons lengthened and he will go back into cast. After reading all of these articles I am a little scared on what will be best for him. It sounds like the risk of having the surgery are just as heavily weighed as just keeping him in AFO\\'s till he can make his own decision. I would like all advice whether it be positive or negative. Thank you in advance for your help.",
         "STORYBRIEF":"Need reassurance that tendon lengthening is the best decision.",
         "ADDEDDATE":"2011-12-12 00:51:16",
         "CURRENTSTATUS":"n"
      }
   }
}
抱歉,我应该添加,在此之前处理jsonResult的代码如下

 try{
            URL url                     = null;
            URLConnection urlConn       = null;
            InputStreamReader jsonIsr   = null;
            BufferedReader jsonBr       = null;

            //this.strDebug += "URL is "+this.strURL+"\n";
            url = new URL(this.strURL);

            urlConn = url.openConnection();

            jsonIsr = new InputStreamReader(urlConn.getInputStream());

            jsonBr = new BufferedReader(jsonIsr, 8192);

            this.jsonResult = jsonBr;

            return true;


        }catch(MalformedURLException e){
            this.strDebug += "JSON Error (1) "+e.getLocalizedMessage()+"\n";
        }catch(IOException e){
            this.strDebug += "JSON Error (2) "+e.getLocalizedMessage()+"\n";
        }
    }else{
        strDebug = "NO URL Passed to JSON\n";
    }
//编辑2

对于那些提出要求的人

错误如标题所示

 Error (4) A JSONObject text must being with '{' at character 1 of {"NumStories":1, "StoryData":........

您正在逐行读取数据,并试图将每一行转换为一个JSON对象。这是行不通的,因为一行只包含一个完整JSON对象的片段

我不知道
jsonResult
是什么类型的。但是你可能想立刻阅读全部内容


您的旧web应用程序可能会生成不带换行符的JSON数据,因此一行就可以包含完整的JSON对象。

我认为您可以逐行读取JSON文件,然后传递给JSON对象。您应该希望通过这种方式将必须传递给JSON对象进行解析的整个字符串传递给JSON对象,而不仅仅是获取JSON

JSONObject arrStories = new JSONObject(jsonResult);
现在像这样得到物体

intNumStories = Integer.parseInt(arrStories.getString("NumStories"));

您的代码假设整个JSON数据都在一行中:它使用
readLine()
进行迭代,但每次都会创建一个新的JSON对象。

如果对象占用多行(显然是这样),则此代码将中断。您的选择是:

  • 将所有字符串收集到字符串生成器中,从该字符串()进行解析
  • 以GSON或我的数据绑定层()为例,将流解析为对象

如果您的数组是关联的(看起来是关联的),您不需要
JSON\u FORCE\u OBJECT
参数。仅当您希望将数字数组转换为对象时才需要。您实际遇到的问题是什么?还不清楚。jsonResult实际包含什么?一旦我获得值intNumStories,我将在该图上设置一个循环,以循环通过arrAllStories,这将返回给我个人故事。从这个角度来看,最好的解决方案是使用像GSON和数据绑定这样的拉式解析器(见链接-有单元测试)。内置JSON解析器在内存利用率方面非常出色。jsonObject中的故事(写在上面的一行)但目前intNumStories甚至不起作用,它落在IOException上,以这种方式将此id转换为整数并打印到Stories中。getJsonObject(“StoryData”)。getJsonObject(“Story0”)。getString(“id”);