How to Json Parsing in android

前端 未结 4 1926
遇见更好的自我
遇见更好的自我 2021-01-07 08:18

http://docs.blackberry.com/sampledata.json

This is my web service and I want to parse and retrieve vehicleType, vehicleColor, fuel, name, experiencePoints, treadType

4条回答
  •  终归单人心
    2021-01-07 08:46

    you can use this class :

    just make the object of this class and call the method!!

    public class JSONParser {
    
    
        private static String result;
        private static String resultArr;
    
        public static String getJSONArr (String json,String tag1,int arrNum)
        {
    
            try {
                JSONArray array = new JSONArray(json);
                for (int i = 0; i < array.length(); i++) {
                    if(i==arrNum){
                        JSONObject row = array.getJSONObject(i);
                    resultArr = row.getString(tag1);
                    }
                }
    
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            return resultArr;
    
        }
    
        public static String getJSONObj (String json,String tag1)
        {
            try {
                JSONObject jObj =  new JSONObject(json);
    
                if(jObj.has(tag1))
                    result = jObj.getString(tag1);
                else
                    Log.d("JSON ERROR", "tag not found");
    
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            return result;
        }
    }
    

提交回复
热议问题