如何从SprinBoot ResponseEntity的角度响应中从JSONObject读取KEY值

时间:2019-04-04 22:33:49

标签: java angular spring-boot angular-httpclient

在我的Spring Boot后端中,我从HashMap创建了一个JSON对象

HashMap<String, ClassData> val = obj.unZipIt(file.getOriginalFilename());
        Gson Jobj = new Gson();
        System.out.println(Jobj.toJson(val));
        return ResponseEntity.status(HttpStatus.OK).body(Jobj.toJson(val));

在我的Angular前端中,我接受JSON对象响应

 obj= {};

.subscribe(event => {
        if (event.type === HttpEventType.UploadProgress) {
          this.progress.percentage = Math.round(100 * event.loaded / event.total);
        } else if (event instanceof HttpResponse) {
          console.log(event.body);
          this.obj['Classes'] = Object.values(event.body);
          console.log(this.obj['Classes']);
          document.getElementById("demo-content").style.display = "none";
        }
      }, (err) => {
        (err.error instanceof Error)
          let message = (`Code ${err.status}, Error was: ${err.error.errorMessage}`);
          alert(message)
      }
    );

当我打印event.body时,它会给出JSON响应

  

{“ FileProcessor”:{“ filepath”:“ C:\ tmp \ untitled \ src \ FileProcessor.java”,“ conceptList”:[]},“ subClass”:{“ filepath”:“ C:\ tmp \ untitled \ src \ subClass.java“,” conceptList“:[{” parentClassName“:” Main“,” parentPath“:” C:\ tmp \ untitled \ src \ Main.java“,” conceptName“:”继承“ ,“ lineNo”:1}]},“ ProjectProcessor”:{“ filepath”:“ C:\ tmp \ untitled \ src \ ProjectProcessor.java”,“ conceptList”:[]},“ Main”:{“ filepath” :“ C:\ tmp \ untitled \ src \ Main.java”,“ conceptList”:[]},“ ProjectReader”:{“ filepath”:“ C:\ tmp \ untitled \ src \ ProjectReader.java”,“ conceptList “:[]}}

当我尝试取出关键值this.obj['Classes'] = Object.values(event.body);时  它给出了与单个字符分开的数组

enter image description here

有一种方法可以从JSON对象获取KEY值

0 个答案:

没有答案