怎么處理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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM