简体   繁体   中英

Android json parsing without array name

I have a Json Array as string without name and I want to parse it how can i do it in android ?

My array :

{"emp_info":[
             {"id":"1","groupe":"1","professeur":"1"},              
             {"id":"2","groupe":"2","professeur":"1"}
]}

This is how you can parse it

Assuming your json string is data

JSONObject jsonObj = new JSONObject(data);
JSONArray empInfo = jsonObj.getJSONArray("emp_info");
for(int i = 0; i < empInfo.length(); i++){
    JSONObject obj = empInfo.getJSONObject(i);
    String id = obj.getString("id");
    String groupe = obj.getString("groupe");
    String professeur = obj.getString("professeur");
}

The example json you gave has a name, but if it doesn't this is how I do it. Using Gson to parse JSON, I use TypeToken to tell the gson builder it's an array.

List<MyObject> jsonObject = new Gson().fromJson(json, new TypeToken<List<MyObject>>().getType());

With the following code you'll have an object representation of your json array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM