栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【无标题】

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【无标题】

java写的JSON例子:
package com.yiheng.test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;
import org.springframework.core.ReactiveAdapterRegistry;

import java.util.*;

public class DemoObject {

    //JSON添加对象
    @Test
    public void addObject() {
        String band = "{'brand':'huawei','color':'blue'}";
        JSonObject band1 = JSON.parseObject(band);
        band1.put("price", 700);
        System.out.println(band1);
        System.out.println(band1.getString("brand"));
        System.out.println(band1.getInteger("price"));
    }

    //JSONObject
    @Test
    public void printObj(){
        String stu_str="{'no':'001','name':'张三','score':{'chinese':90,'math':'90'}}";
        JSonObject stu_obj = JSON.parseObject(stu_str);
        JSonObject score = stu_obj.getJSONObject("score");
        System.out.println(score.getInteger("chinese"));
        System.out.println(score.getString("math"));
    }

    //JSONArray
    @Test
    public void printArr(){
        String class_str="{'cname':'高一三班','student':[{'no':'001','name':'yiheng'},{'on':'002','name':'哇哈哈'}]}";
        JSonObject class_obj = JSON.parseObject(class_str);
        String student = class_obj.getString("student");
        JSonArray student_arr = JSON.parseArray(student);

        System.out.println(student_arr);
        for(int i=0;i map = new HashMap<>();
        map.put("name","yiheng");
        map.put("sex","man");
        map.put("age","22");
        Iterator> iterator = map.entrySet().iterator();
        while(iterator.hasNext()){
            Map.Entry node=iterator.next();
            System.out.println(node.getKey()+":"+node.getValue());
        }
    }

    //访问LIst
    @Test
    public void DemoList(){
        List list = new ArrayList<>();
        for(int i=0;i<3;i++) {
            list.add("list" + i);
        }

        //方法一
        System.out.println("List访问方法一");
        for(int i=0;i ite=list.iterator();
        while (ite.hasNext())
        {
            System.out.println(ite.next());
        }

        //方法三
        System.out.println("list访问方法三");
        for(String node:list){
            System.out.println(node);
        }
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/759730.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号