springboot string转json json里面带数组

  • json示例
 [
    {source:0,target:4,relation:"籍贯",value:1.3},
    {source:4,target:5,relation:"舍友",value:1},
    {source:4,target:6,relation:"舍友",value:1},
    {source:4,target:7,relation:"舍友",value:1},
    {source:1,target:6,relation:"籍贯",value:2},
    {source:2,target:5,relation:"籍贯",value:0.9},
    {source:3,target:7,relation:"籍贯",value:1},
    {source:5,target:6,relation:"同学",value:1.6},
    {source:6,target:7,relation:"朋友",value:0.7},
    {source:6,target:8,relation:"职责",value:2}
]
  • 我的需求是将string类型转化为json数组
添加依赖
        
        
            net.sf.json-lib
            json-lib
            2.4
            jdk15
        
  • 使用这个包进行解析
         String string = " [\n" +
                 "    {source:0,target:4,relation:\"籍贯\",value:1.3},\n" +
                 "    {source:4,target:5,relation:\"舍友\",value:1},\n" +
                 "    {source:4,target:6,relation:\"舍友\",value:1},\n" +
                 "    {source:4,target:7,relation:\"舍友\",value:1},\n" +
                 "    {source:1,target:6,relation:\"籍贯\",value:2},\n" +
                 "    {source:2,target:5,relation:\"籍贯\",value:0.9},\n" +
                 "    {source:3,target:7,relation:\"籍贯\",value:1},\n" +
                 "    {source:5,target:6,relation:\"同学\",value:1.6},\n" +
                 "    {source:6,target:7,relation:\"朋友\",value:0.7},\n" +
                 "    {source:6,target:8,relation:\"职责\",value:2}\n" +
                 "]";
        JSONArray jsonArray = JSONArray.fromObject(string);

你可能感兴趣的:(java,springboot,学生,springboot,string转json,json里面带数组)