我有一个显示JSON数组的PHP API,然后我将其读入Android应用程序。
我自从移动了服务器并且android应用程序崩溃了。
我认为这是认证,并认为我会重新构建Android应用程序(是我的第一个应用程序,并认为重写可以使事情变得更好)
出于某种原因,我现在收到此异常错误
我在某处读到了我需要解析PHP json_encode中的JSON_FORCE_OBJECT
json_encode($arrMyData, JSON_FORCE_OBJECT);
但是我正在运行PHP 5.2(在PHP 5.3中出现了Options参数)
我的代码供您参考
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":........
答案 0 :(得分:2)
您的代码假定整个JSON数据都在一行上:它以readLine()
进行迭代,但每次都会创建一个新的JSON对象。
答案 1 :(得分:0)
您正在逐行读取数据并尝试将每一行转换为JSON对象。这不会起作用,因为一行只包含一个完整JSON对象的片段。
我不知道jsonResult
的类型。但是你可能想立刻阅读整篇文章。
您的旧Web应用程序可能在没有换行符的情况下生成JSON数据,因此一行将包含完整的JSON对象。
答案 2 :(得分:0)
我认为你逐行读取json文件并传递给json对象你应该这样整个字符串你必须传递给json对象进行解析而不仅仅是你得到了json
JSONObject arrStories = new JSONObject(jsonResult);
现在以这种方式获取对象
intNumStories = Integer.parseInt(arrStories.getString("NumStories"));
答案 3 :(得分:0)
如果对象需要多行(很可取),这段代码将会中断。您的选择是: