NotificationNotificationNotification
import org.json.JSONException;
import org.json.JSONObject;
//先把字符串转化为数组,然后遍历数组,在提取
JSONObject jstr = null;
try {
jstr = new JSONObject(arg1);
} catch (JSONException e) {
e.printStackTrace();
}
String game_serverid = null;
try {
game_serverid = jstr.getString(“game_serverid”);
} catch (JSONException e) {
e.printStackTrace();
}
String game_userid = null;
try {
game_userid = jstr.getString(“game_userid”);
} catch (JSONException e) {
e.printStackTrace();
}
方法1:割断字符串
删除前后的中括号,将{}提取出来(匹配”},{“来找到分界点,然后割断字符窜,行成一个数组,这些数组里面添加就是json),里面是有key:value的,
方法2:插入法
在每一个大段没有key的{前,插入自己的key,可以是这样:[1:{…},2:{…}, n:{…}],然后在用我上面给的方法。
这两种方法仅限于你这类比较简单的json的。
JSONObject job=…
JSONArray jarr = job.names();
试试看
jsonObject.optString(“screen_name”,””)即可;
jsonObject = new JSONObject(contents.trim());
Iterator<?> keys = jsonObject.keys();
while( keys.hasNext() ){
String key = (String)keys.next();
if( jsonObject.get(key) instanceof JSONObject ){
}
}
http://stackoverflow.com/questions/9151619/java-iterate-over-jsonobject/10593838#10593838
List newList=new;
newList = new ArrayList();
try{
JSONArray jsonArray = new JSONArray(arg2); //获取到的String转换为JsonArray
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject item = jsonArray.getJSONObject(i); // 得到每个对象
News news=new News(); //新建一个类 类的字段 对应每个对象上的key(字段数量不限)
news.setId(item.getString(“id”)); //id 就是数组上每个对象内的key
news.setCode(item.getString(“code”));
news.setName(item.getString(“name”));
news.setCreatedTime(item.getString(“createdTime”));
newList.add(news); //newList 是一个数组 类型为
}
你可以看一下数据,你返回数据是不正确的的。应该是服务器端出错了,返回了错误提示,要么就是你地址用错了。
它都提示你<html 了,很明示这是html的标签,说明返回的数据就是个标准的网页,根本就不是JSON。