怎么处理json字符串

1,可以先进行判断,是jsonObject:{"":""}还是Arrayjson:[{},{}],使用JSONTokener(自己知道的情况下就简单一些)

ArrayList<Object> arrayList = new ArrayList<>();
        try {
            Object json=new JSONTokener(response).nextValue();
            if(json instanceof JSONArray){
                JSONArray array=new JSONArray(response);
                for (int i=0;i<array.length();i++){
                    arrayList.add(array.get(i));
                }
                return arrayList;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

2,自己写一个类,处理json

package com.example.fitness_app;

import com.example.fitness_app.bean.FitnessCourse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

public class MyJson {
    //返回json对象
   public JSONObject JsonToObject(String response){

       try {

           Object json=new JSONTokener(response).nextValue();
           if(json instanceof JSONObject){
               JSONObject jso = new JSONObject(response);
               return jso;
           }
       } catch (JSONException e) {
           e.printStackTrace();
       }

       return null;
   }

   //返回list集合
    public List<Object> JsonToList(String response){
        ArrayList<Object> arrayList = new ArrayList<>();
        try {
            Object json=new JSONTokener(response).nextValue();
            if(json instanceof JSONArray){
                JSONArray array=new JSONArray(response);
                for (int i=0;i<array.length();i++){
                    arrayList.add(array.get(i));
                }
                return arrayList;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return  null;
    }

    //返回list集合
    public List<FitnessCourse> JsonToListTeacherCourse(String response){
        ArrayList<FitnessCourse> arrayList = new ArrayList<>();
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Object json=new JSONTokener(response).nextValue();
            if(json instanceof JSONArray){
                JSONArray array=new JSONArray(response);

                for (int i=0;i<array.length();i++){
                    FitnessCourse  f = new FitnessCourse();
                    JSONObject jsonObject = array.getJSONObject(i);
                    if(jsonObject.get("id")!=null){
                        f.setId(Long.parseLong(jsonObject.get("id").toString()));
                    }
                    if (jsonObject.get("name")!=null){
                        f.setName((String) jsonObject.get("name"));
                    }
                    if (jsonObject.get("description")!=null){
                        f.setDescription((String) jsonObject.get("description"));
                    }
                    if (jsonObject.get("courseDay")!=null){
                        f.setCourseDay((String) jsonObject.get("courseDay"));
                    }
                    if (jsonObject.get("courseTime")!=null){
                        f.setCourseTime((String) jsonObject.get("courseTime"));
                    }
                    if (jsonObject.get("teacherId")!=null){
                        f.setTeacherId(Long.parseLong(jsonObject.get("teacherId").toString()));
                    }
                    arrayList.add(f);
                }
                return arrayList;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return  null;
    }

3,在带有泛型的情况下不能强转!(不带的情况下,子父类可以互相强转!!)

4,可以通过循环单个取出强转

 List<FitnessCourse> courses = myJson.JsonToListTeacherCourse(t_course);
        List<Object> list = myJson.JsonToList(t_course);
        ArrayList<FitnessCourse> courses1 = new ArrayList<>();
        for (int i=0;i<list.size();i++){
            FitnessCourse o = (FitnessCourse)list.get(i);
        }

链接:https://blog.csdn.net/rlingge/article/details/49883705

猜你喜欢

转载自www.cnblogs.com/wskb/p/12444863.html