本文介绍了什么是机器人(JSON)错误解析数据org.json.JSONException错误表示:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
错误分析数据org.json.JSONException:
java.lang.String类型的值不能转换为JSONArray
JSONArray jsonArray =新JSONArray(结果);
JSONArray JA =(JSONArray),其中jsonArray.get(0);
JB的JSONObject =(JSONObject的)ja.get(0);
//串firstvalue = jb.getString(0);
INT secondvalue = jb.getInt(CUSTOMER_ID);
Myjson输出应该像 [{0:2,CUSTOMER_ID:2}]
我正在jenerating使用PHP输出像
$输出[] = $客户ID;
打印(json_en code($输出));
解决方案
要你分析问题的已经回答@
How调用Android中的JSON对象
其字符串
字符串secondvalue = jb.getString(CUSTOMER_ID);
尝试
{
int值=的Integer.parseInt(secondvalue);
Log.i(客户ID,+值);
}赶上(NumberFormatException的E)
{
e.printStackTrace();
}
编辑:
字符串myjson =[
+[
+{
+\\0 \\:\\2 \\,
+\\CUSTOMER_ID \\:\\2 \\
+}
+]
+];
解析
{尝试
JSONArray jsonArray =新JSONArray(myjson);
JSONArray JA =(JSONArray),其中jsonArray.get(0);
JB的JSONObject =(JSONObject的)ja.get(0);
串firstvalue = jb.getString(0);
串secondvalue = jb.getString(CUSTOMER_ID);
int值=的Integer.parseInt(secondvalue);
Log.i(客户ID,+值);
Log.i(第一个值,firstvalue);
Log.i(第二个值是secondvalue);
}赶上(NumberFormatException的E)
{
e.printStackTrace();
}赶上(例外五){
// TODO自动生成catch块
e.printStackTrace();
}
Error parsing data org.json.JSONException:
Value of type java.lang.String cannot be converted to JSONArray
JSONArray jsonArray = new JSONArray(result);
JSONArray ja= (JSONArray) jsonArray.get(0);
JSONObject jb = (JSONObject) ja.get(0);
//String firstvalue = jb.getString("0");
int secondvalue = jb.getInt("customer_id");
Myjson output should like [[{"0":"2","customer_id":"2"}]]
i am jenerating the output using the php like
$output[]= $customerid;
print(json_encode($output));
解决方案
To your parsing problem its already answered @
How to call the JSON object in Android
Its a string
String secondvalue = jb.getString("CUSTOMER_ID");
try
{
int value = Integer.parseInt("secondvalue");
Log.i("Customer id",""+value);
}catch(NumberFormatException e)
{
e.printStackTrace();
}
Edit:
String myjson = "["
+"["
+"{"
+ " \"0\": \"2\","
+ "\"CUSTOMER_ID\": \"2\""
+ "}"
+ "]"
+ "]";
Parsing
try {
JSONArray jsonArray = new JSONArray(myjson);
JSONArray ja= (JSONArray) jsonArray.get(0);
JSONObject jb = (JSONObject) ja.get(0);
String firstvalue = jb.getString("0");
String secondvalue = jb.getString("CUSTOMER_ID");
int value = Integer.parseInt(secondvalue);
Log.i("Customer id",""+value);
Log.i("first value is",firstvalue);
Log.i("second value is",secondvalue);
}catch(NumberFormatException e)
{
e.printStackTrace();
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这篇关于什么是机器人(JSON)错误解析数据org.json.JSONException错误表示:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!