Android 解析时出现异常 JSON URL
Android Exception while parsing JSON URL
我正在尝试解析来自 URL 的 JSON 响应并将其填充到数组中。尝试解析我的 json 响应 URL:
时出现此异常
org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONArray
这是我的回复:
{"firstName":"Roger","lastName":"Davis","email":"rog@gmail.com","password":"test123","profilePicture":"googleimagesblablabla","settings":[]}
我的代码:
URL url = new URL(myurl);
HttpURLConnection urlRequest = (HttpURLConnection) url.openConnection();
urlRequest.setRequestMethod("GET");
urlRequest.addRequestProperty("Content-Type", "application/json");
urlRequest.addRequestProperty("ACCEPT", "application/json");
urlRequest.addRequestProperty("X-ZUMO-APPLICATION", mobileServiceAppId);
urlRequest.addRequestProperty("email",email);
urlRequest.addRequestProperty("password",pass);
JSONObject[] response = null;
try {
// Prepare some objects to receive the bytes of data
// from the Azure Mobile Service
InputStream in = new BufferedInputStream(
urlRequest.getInputStream());
BufferedReader bufferReader = new BufferedReader(
new InputStreamReader(in));
// responseString will hold our JSON data
StringBuilder responseString = new StringBuilder();
String line;
// Loop through the buffered input, reading JSON data
while ((line = bufferReader.readLine()) != null) {
responseString.append(line);
}
// Convert responseString into a JSONArray
JSONArray jsonArray = new JSONArray(responseString.toString());
Toast.makeText(getApplicationContext(),jsonArray.length(),Toast.LENGTH_LONG).show();
for(int i=0;i<jsonArray.length();i++)
{
JSONObject obj = jsonArray.getJSONObject(i);
Log.d("ERROR",obj.getString("lastName"));
}
}
catch(Exception ex)
{
Log.d("ERROR", ex.toString());
}
我是否从响应中解析了错误的值?伙计们,我该如何解决这个问题?
这不是 JSON 数组,JSON 数组以“[”开头,以“]”结尾
{"firstName":"Roger","lastName":"Davis","email":"rog@gmail.com","password":"test123","profilePicture":"googleimagesblablabla"}
所以你应该改用JSONObject
实际上您的 json 响应中没有任何数组:
{"firstName":"Roger","lastName":"Davis","email":"rog@gmail.com","password":"test123","profilePicture":"googleimagesblablabla"}
它包含响应中的所有对象,您可以从下面给定的代码片段中解析您的响应。
JSONObject reader = new JSONObject(responseString.toString());
String firstName = reader.getString("firstName");
String lastName= reader.getString("lastName");
String email= reader.getString("email");
String password= reader.getString("password");
String profilePicture= reader.getString("profilePicture");
并从您的代码中删除此部分:
// Convert responseString into a JSONArray
JSONArray jsonArray = new JSONArray(responseString.toString());
Toast.makeText(getApplicationContext(),jsonArray.length(),Toast.LENGTH_LONG).show();
for(int i=0;i<jsonArray.length();i++)
{
JSONObject obj = jsonArray.getJSONObject(i);
Log.d("ERROR",obj.getString("lastName"));
}
我正在尝试解析来自 URL 的 JSON 响应并将其填充到数组中。尝试解析我的 json 响应 URL:
时出现此异常org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONArray
这是我的回复:
{"firstName":"Roger","lastName":"Davis","email":"rog@gmail.com","password":"test123","profilePicture":"googleimagesblablabla","settings":[]}
我的代码:
URL url = new URL(myurl);
HttpURLConnection urlRequest = (HttpURLConnection) url.openConnection();
urlRequest.setRequestMethod("GET");
urlRequest.addRequestProperty("Content-Type", "application/json");
urlRequest.addRequestProperty("ACCEPT", "application/json");
urlRequest.addRequestProperty("X-ZUMO-APPLICATION", mobileServiceAppId);
urlRequest.addRequestProperty("email",email);
urlRequest.addRequestProperty("password",pass);
JSONObject[] response = null;
try {
// Prepare some objects to receive the bytes of data
// from the Azure Mobile Service
InputStream in = new BufferedInputStream(
urlRequest.getInputStream());
BufferedReader bufferReader = new BufferedReader(
new InputStreamReader(in));
// responseString will hold our JSON data
StringBuilder responseString = new StringBuilder();
String line;
// Loop through the buffered input, reading JSON data
while ((line = bufferReader.readLine()) != null) {
responseString.append(line);
}
// Convert responseString into a JSONArray
JSONArray jsonArray = new JSONArray(responseString.toString());
Toast.makeText(getApplicationContext(),jsonArray.length(),Toast.LENGTH_LONG).show();
for(int i=0;i<jsonArray.length();i++)
{
JSONObject obj = jsonArray.getJSONObject(i);
Log.d("ERROR",obj.getString("lastName"));
}
}
catch(Exception ex)
{
Log.d("ERROR", ex.toString());
}
我是否从响应中解析了错误的值?伙计们,我该如何解决这个问题?
这不是 JSON 数组,JSON 数组以“[”开头,以“]”结尾
{"firstName":"Roger","lastName":"Davis","email":"rog@gmail.com","password":"test123","profilePicture":"googleimagesblablabla"}
所以你应该改用JSONObject
实际上您的 json 响应中没有任何数组:
{"firstName":"Roger","lastName":"Davis","email":"rog@gmail.com","password":"test123","profilePicture":"googleimagesblablabla"}
它包含响应中的所有对象,您可以从下面给定的代码片段中解析您的响应。
JSONObject reader = new JSONObject(responseString.toString());
String firstName = reader.getString("firstName");
String lastName= reader.getString("lastName");
String email= reader.getString("email");
String password= reader.getString("password");
String profilePicture= reader.getString("profilePicture");
并从您的代码中删除此部分:
// Convert responseString into a JSONArray
JSONArray jsonArray = new JSONArray(responseString.toString());
Toast.makeText(getApplicationContext(),jsonArray.length(),Toast.LENGTH_LONG).show();
for(int i=0;i<jsonArray.length();i++)
{
JSONObject obj = jsonArray.getJSONObject(i);
Log.d("ERROR",obj.getString("lastName"));
}