×

android jsonobject

android jsonobject(android怎么遍历jsonobject)

admin admin 发表于2023-04-26 03:39:23 浏览31 评论0

抢沙发发表评论

本文目录

android怎么遍历jsonobject


android 读取json数据(遍历JSONObject和JSONArray)
•public String getJson(){
• String jsonString = “{\“FLAG\“:\“flag\“,\“MESSAGE\“:\“SUCCESS\“,\“name\“:[{\“name\“:\“jack\“},{\“name\“:\“lucy\“}]}“;//json字符串
• try {
• JSONObject result = new JSONObject(jsonstring);//转换为JSONObject
• int num = result.length();
• JSONArray nameList = result.getJSONArray(“name“);//获取JSONArray
• int length = nameList.length();
• String aa = ““;
• for(int i = 0; i 《 length; i++){//遍历JSONArray
• Log.d(“debugTest“,Integer.toString(i));
• JSONObject oj = nameList.getJSONObject(i);
• aa = aa + oj.getString(“name“)+“|“;

• }
• Iterator《?》 it = result.keys();
• String aa2 = ““;
• String bb2 = null;
• while(it.hasNext()){//遍历JSONObject
• bb2 = (String) it.next().toString();
• aa2 = aa2 + result.getString(bb2);

• }
• return aa;
• } catch (JSONException e) {
• throw new RuntimeException(e);
• }
• }

Android开发能不能使用net.sf.json.JSONObject解析JSON


你需要确保你的程序在打包生成apk文件时,把你所需的类库(如你这个net。sf。json)jar包一并包装进去。。。
或者你如果能确保目标手机的
system/lib
(具体不是这个路径)里面有这个jar,也可以。

Android Json解析


用JSONObject 这个开源库,或者用android的 Gson开源库

JSONObject jsonObj = new JSONObject(json字符串);

JSONArray array = jsonObj.getJSONArray(“result“);


android中jsonobject和jsonobject的区别


1.JSONObject是Android原生的json类,通过import org.json.JSONObject来导入。
JsonObject需要添加gson jar包,通过com.google.gson.JsonObject来导入。
2.查看源码,可以看到
JSONObject通过HashMap来保存键值对。
JsonObject使用LinkedTreeMap来保存键值对。
3.JSONObject:添加value为null的键值对,Map保存的时候会删掉这一键值对;
JsonObject:添加value为null的键值对,Map会保留value值是null的键值对。

android怎么解析json文件


你好,我试过了,这样能取到你要的结果:

用的gson-2.2.4.jar包,你应该有吧,没有百度搜下去下个就好了。

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class MyTest {
    public static void main(String args) {
        String json = “{’resultcode’:’200’,’reason’:’ReturnSuccessd!’,’result’:{’data’:[{’MCC’:’460’,’MNC’:’1’,’LNG’:’120.721423’,’LAT’:’31.29854’,’O_LNG’:’120.72577772352’,’O_LAT’:’31.296529947917’,’PRECISION’:’1101’,’ADDRESS’:’江苏省苏州市吴中区金鸡湖大道368号’}]}}“;
        JsonParser jsonParser = new JsonParser();
        JsonObject jsonObj = jsonParser.parse(json).getAsJsonObject();
        JsonObject result = jsonObj.get(“result“).getAsJsonObject();
        JsonArray data = result.get(“data“).getAsJsonArray();
        String O_LNG = data.get(0).getAsJsonObject().get(“O_LNG“).getAsString();
        String O_LAT = data.get(0).getAsJsonObject().get(“O_LAT“).getAsString();
        String ADDRESS = data.get(0).getAsJsonObject().get(“ADDRESS“).getAsString();
        System.out.println(O_LNG);
        System.out.println(O_LAT);
        System.out.println(ADDRESS);
    }
}

jsonobject解析 跪求解答java、android


jsonObject jsonobject=new jsonObject(str);//str表示你取到的内容
String name=jsonobject.getstring(“name“);
jsonObject jsonobject1=jsonobject.getjsonObject(“data“);
jsonObject jsonobject=new jsonObject
List《String 》 listname=new ArrayList《String》();
for(int i=7;i《=17;i++){
if(jsonobject.has(““+i)){
jsonObject jsonobject[i-7]=jsonoject1.getjsonObject(““+i);
String ctiyname=jsonobject[i-7].getString(“name“);
listname.add(cityname);
}
}

android jsonarry 怎么放jsonobject里面


试试把你的这个字符串复制到notepad++里面,看是不是有隐藏的字符没有显示出来,或者试试: new JSONObject(json.substring(json.indexOf(“{“), json.lastIndexOf(“}“) + 1));