Android开发,关于Json的解析

Json

本人纯属小白,本博客只是个人记录,不推荐作为参考。
Json作为一种新型轻量级数据类型,对于数据上传等,服务器返回数据等方面应用非常广泛,从一定程度上提升了数据加载效率。

  1. 将以下Json类型数据以对象的形式进行载入

在这里插入图片描述
以对象形式进行载入:

  1. 最里层
public class MostLess(){
    
    
   private String name;
   private int value;
   private String minute;
   public MostLess(String c_name,int c_value,String c_minute){
    
    
       name = c_name;
       value = c_value;
       minute = c_minute;
   }
}
  1. time类,防与系统time重名
public class Timer(){
    
    
     private MostLess total;
     private List<MostLess> data;
     public Timer(MostLess c_mostless,List<MostLess> c_listmostless){
    
    
         total = c_mostless;
         data = c_listmostless;
     }
}

3.最外层类

public class MostOut(){
    
    
     private String name;
     private String author;
     private List<String> content;
     private Timer time;
     public MostOut(String c_name,String c_author,List<String> c_listString,Timer c_time){
    
    
           name = c_name;
           author = c_author;
           content = c_listString;
           time = c_time;         

     }
     
}
  1. xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".JsonLearnActivity">
     <TextView
         android:id="@+id/textview"
         android:layout_gravity="center"
         android:textSize="20dp"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" />
</LinearLayout>
  1. Activity.class
//细节就不写了
public void init(){
    
    
   textview = (TextView)findViewById(R.id.textview);
   //先从最里层开始写起
   MostLess total = new MostLess("All",200,"minute");
   MostLess mostLess1 = new MostLess("http基础",60,"minute");
   MostLess mostLess2 = new MostLess("json数据解析",50,"minute");
   //data的list
   List<MostLess> data = new ArrayList<MostLess>();
   data.add(mostless1);
   data.add(mostless2);
   //time的建立
   Timer time = new Timer(total,data);
   //content的建立
   String[] arr = new String[]{
    
    "http基础","json解析",
   "封装okhttp","经验分享","总结"};
   List<String> content = new ArrayList<String>();
   for(int i=0;i<arr.length();i++){
    
    
      content.add(arr[i]);
   }
   //MostOut对象的建立
   MostOut mostOut = new MostOut(name,author,content,time);
   //Gson开源框架将其转化为json形式
   Gson gson = new Gson();
   String jsonTranslateByGson = gson.getJson(mostOut);
   //在控件中显示
   textview.settext(jsonTranslateByGson);


}

对于Gson依赖包,网上有很多,自行下载哦
要是你觉得代码不规范,对于json数据修改不方便,那你添加个、set,get方法吧。
—————————————————————————

非对象形式,直接以String类型转化为json

String name = "Fxy";
String gender = "man";
int age = 21;
// 一般的string本身是 \"String\"
//如果是变量,那么要加 ”+变量名+”
String str ="{\"id\":5120175994,\"name\":\""+name+"\",
\"gender\":\""+gender+"\",\"age\":\""+age+"\"}";

—————————————————————————

解析Json

这个也简单,就是把string转化为对象,以对象的引用获得其值,像上面的样。

Gson gson = new Gson();
//实例化一个接收对象,
MostOut mostout1 = gson.fromJson(jsonTranslateByGson,MostOut.class);
System.out.println("name:"+mostout1.name);

本人纯属小白,本博客只是个人记录,不推荐作为参考。

猜你喜欢

转载自blog.csdn.net/qq_41904106/article/details/108366733