常见JSON解析

public class JsonTest {

    public static void main(String[] args) {

        //JSON 解析
        String body = "{\"goodsName\":\"测试123241\",\"imgs\":\"https://mszhibojkfztc.oss-cn-shenzhen.aliyuncs.com/jipinhui/20210912/01c8415b10ec7aa80121245d57334fdc.png@2o.png\",\"attribute\":\"[2,5,7]\",\"specValue\":[{\"s0\":\"c1\",\"s1\":\"k1\",\"price\":\"1212\",\"img\":\"https://mszhib45ojkfztc.oss-cn-shenzhen.aliyuncs.com/jipi45nhui/20210912/01c841455b10ec7aa801212d57334fdc.png@2o.png\"},{\"s0\":\"c2\",\"s1\":\"k1\",\"price\":\"12312\",\"img\":\"https://mszhibojkfztc.oss-cn-shenzhen.aliyuncs.com/jipinhui/20210912/01c8415b10ec7aa801212d57334fdc.png@2o.png\"}],\"productImgs\":\"[\\\"https://mszhibojkfztc.oss-cn-shenzhen.aliyuncs.com/jipinhui/20210912/01c8415b10ec7aa801212d57334fdc.png@2o.png\\\",\\\"https://mszhibojkfztc.oss-cn-shenzhen.aliyuncs.com/jipinhui/20210912/Snipaste_2020-04-07_14-22-17.png\\\",\\\"https://mszhibojkfztc.oss-cn-shenzhen.aliyun45cs.com/jipinhui/2045210912/捕获.jpg\\\"]\",\"imageText\":\"<p>1231231231231231</p>\",\"storeId\":26,\"shelfType\":1}";
        JSONObject jsonObject = JSONObject.parseObject(body);
        String specValue = jsonObject.getString("specValue");
        //解析成数组
        JSONArray jsonArray = JSONObject.parseArray(specValue);
        for (int i = 0; i < jsonArray.size(); i++) {
            String s = jsonArray.get(i).toString();
            String img = JacksonUtil.parseString(s, "img");
            System.out.println(img);
            System.out.println(jsonArray.get(i));
        }

        //String 转 数组
        String a = "[1,2,3]";
        JSONArray jsonArray1 = JSONObject.parseArray(a);
        List<Integer> objects = new ArrayList<>();
        for (Object o : jsonArray1) {
            objects.add(Integer.parseInt(o.toString()));
            System.out.println(o);
        }


    }
}

@Test
public void testQueryWarrper()  {

    List<JyStore> list = storeService.list();
    String companys = JSON.toJSONString(list);
    System.out.println(companys);
    List<JyStore> students = JSON.parseObject(companys,new TypeReference<List<JyStore>>(){});

    students.forEach(o-> System.out.println(o.toString()));




}
package com.qqwl.gold;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.qqwl.gold.pojo.JyStore;
import com.qqwl.gold.service.JyStoreService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

/**
 * 测试类, 用户测试方法
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = AdminApplication.class)
public class ThisTestone {


    @Autowired
    private JyStoreService storeService;



    @Test
    public void testQueryWarrper()  {

        List<JyStore> list = storeService.list();
        String companys = JSON.toJSONString(list);
        System.out.println(companys);
        List<JyStore> students = JSON.parseObject(companys,new TypeReference<List<JyStore>>(){});

        students.forEach(o-> System.out.println(o.toString()));




    }



}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值