Json简介

Posted 浅滩浅

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Json简介相关的知识,希望对你有一定的参考价值。






Book实体类

public class Book {
    private String category;
    private String tittle;
    private String author;
    private String year;
    private String price;

    public Book() {
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getTittle() {
        return tittle;
    }

    public void setTittle(String tittle) {
        this.tittle = tittle;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }
}

Person实体类

import java.util.List;

public class Person {
    private String name;
    private int age;
    private List<Integer> scores;

    public Person() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public List<Integer> getScores() {
        return scores;
    }

    public void setScores(List<Integer> scores) {
        this.scores = scores;
    }
}

book.json

[
  {"category":"科幻","tittle":"三体","author":"lisi","year":"2018/01/05","price":"28"},
  {"category":"科幻","tittle":"四体","author":"lisi","year":"2018/01/05","price":"28"}
]

OrgJson进行解析




Gson

因为之前用过Google的Gson所以在这里给出代码

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.io.File;
import java.io.FileReader;
import java.util.Arrays;
import java.util.List;

public class JsonTest {

    public static void main(String[] args) {

        testJsonObject();
        testJsonFile();
    }

    public static void testJsonObject(){
        //构造对象
        Person p = new Person();
        p.setName("zhangsan");
        p.setAge(18);
        p.setScores(Arrays.asList(60,70,80));

        //从java对象到json字符串
        Gson gson = new Gson();
        String str = gson.toJson(p);
        System.out.println(str);

        //从json字符串到Java对象
        Person p1 = gson.fromJson(str,Person.class);
        System.out.println(p1.getName());//zhangsan
        System.out.println(p1.getAge());//18
        System.out.println(p1.getScores());//[60,70,80]

        //调用GSON的JsonObject
        JsonObject json = gson.toJsonTree(p).getAsJsonObject();//将json解析为一棵树
        System.out.println(json.get("name"));//"zhangsan"
        System.out.println(json.get("age"));//18
        System.out.println(json.get("scores"));//[60,70,80]


    }

    public static void testJsonFile(){
    //从json文件进行加载并重构为java对象
        Gson gson = new Gson();
        File file = new File("book.json");
        try(FileReader reader = new FileReader(file)){
            List<Book> books = gson.fromJson(reader,new TypeToken<List<Book>>(){}.getType());
            for(Book book:books){
                System.out.println(book.getAuthor()+" "+book.getTittle());
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

}

Jackson



以上是关于Json简介的主要内容,如果未能解决你的问题,请参考以下文章

json 可视代码工作室Angular with Firebase片段

vs code 用户代码片段 html.json

Android 逆向类加载器 ClassLoader ( 类加载器源码简介 | BaseDexClassLoader | DexClassLoader | PathClassLoader )(代码片段

错误代码:错误域 = NSCocoaErrorDomain 代码 = 3840“JSON 文本没有以数组或对象和允许未设置片段的选项开头。”

如何在android中将json数据加载到片段中

使用 json rereiver php mysql 在片段中填充列表视图