【问题标题】:return only one json string in http call angular 4在 http 调用 Angular 4 中只返回一个 json 字符串
【发布时间】:2018-02-09 23:35:54
【问题描述】:

我想在 JSON 调用中将字符串返回到路径。然而,这个 JSON 调用中有很多变量。

@Injectable()
export class ImageHolderService {

    private studentPath: string;

    constructor(private http: HttpUtil, private appConstants: AppConstants){

    }

    getAvatar():string {
        return this.http.get(this.appConstants.GRAVTAR_URL_FOR_PROFILE_IMAGE+"/leaderbaord").subscribe()(response => {

            });
    }

}

我怎样才能从以下 JSON 调用中返回一个字符串:

假设 JSON 是正确的,这只是一个小例子:

[ {
  "sumsPointValue" : 95,
  "student" : {
    "id" : 1,
    "studentName" : "Joe Buck",
    "linkedInProfile" : "http://linkedin.com",
    "twitterProfile" : null,
    "githubProfile" : "http://github.com",
    "stackoverflowProfile" : "http://stackoverflow.com",
    "email" : "joe@example.com",
    "teachableId" : null,
    "teachableSchoolId" : null,
    "studentImage" : null,
    "studentLevel" : {
      "id" : 1,
      "awardDate" : "2017-08-31T18:13:38.688-04:00",
      "level" : {
        "id" : 1,
        "description" : "This guy is on Level 0 description which is being pulled from the database and representing a longer than average text string to ensure the view is properly working.",
        "imageName" : "guru-level-0.png",
        "levelNumber" : 0,
        "created" : "2017-08-31T22:13:38.570+0000",
        "updated" : "2017-08-31T22:13:38.570+0000"
      },
      "created" : "2017-08-31T22:13:38.694+0000",
      "updated" : "2017-08-31T22:13:38.695+0000"
    },
    "created" : "2017-08-31T22:13:38.684+0000",
    "updated" : "2017-08-31T22:13:38.824+0000"
  },
  "studentLevelImagePath" : "../assets/images/level/level-0.png",
  "studentImagePath" : "../assets/images/avatar.png",
  "kudosGiven" : 0,
  "badgesEarned" : 2,
  "kudosRecieved" : 0
},

我要"studentImagePath" : "../assets/images/avatar.png",

----------------更新一个----------------

我做了以下事情:

getAvatar():string {
        return <string>this.http.get(this.appConstants.GRAVTAR_URL_FOR_PROFILE_IMAGE + "/leaderbaord").map(response => response.json()).subscribe(data => {
            console.log(data.studentImagePath);
        });
    }

使用时出现错误,是:

common/image.holder/imageHolderService.ts (12,16): Type 'Subscription' cannot be converted to type 'string'.

我是这样使用的:

  constructor(private leaderBoardService: LeaderBoardService, private userPicture: ImageHolderService) { }

    ngOnInit() {
        this.subscribeLeaderBoardDataFromStore();
        this.pictureOfUser = this.userPicture.getAvatar();
    }

【问题讨论】:

  • 你得到的是一组对象,还是只有一个对象?如果它是一个对象数组,你想要什么?一个字符串数组?看起来有一个数组进来了,因为你的 JSON 以 [ 开头,但我有点不清楚:)

标签: angular


【解决方案1】:

我认为这应该可行:

this.http.get(this.appConstants.GRAVTAR_URL_FOR_PROFILE_IMAGE+"/leaderbaord").map(res => res.json()).subscribe(data => {
    console.log(data.studentImagePath);
});  

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2018-02-08
  • 2018-01-23
  • 1970-01-01
  • 2022-08-22
  • 2011-07-10
  • 1970-01-01
  • 2020-01-20
  • 2023-04-09
相关资源
最近更新 更多