首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从服务调用中设置我的数组

如何从服务调用中设置我的数组
EN

Stack Overflow用户
提问于 2021-01-06 02:35:42
回答 1查看 42关注 0票数 1

我有一个数组,我正在进行一个服务调用,我想将所有返回的json数组设置为我的,这是到目前为止的代码:

代码语言:javascript
复制
interface QuestionHeader {
  id: number;
  questionName: string;
  Question: Question;
}

const [questions, setQuestions] = useState<QuestionHeader[]>([]);
  function runService() {
    const requestOptions = {
        method: 'GET',
        headers: { 'Content-Type': 'application/json' }
    };
    fetch(`URL`, requestOptions)
    .then(res => res.json())
    .then(e => {
      setQuestions()

    });
  }

这是JSON:{ "id":2,"questionID":2,"questionName":“客户满意”,“问题”:{ "questionID":2,"questionContent":“品牌满意度”}}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-06 02:40:36

你可以这样做。

代码语言:javascript
复制
const [questions, setQuestions] = useState<QuestionHeader[]>([]);
  function runService() {
    const requestOptions = {
        method: 'GET',
        headers: { 'Content-Type': 'application/json' }
    };
    fetch("URL", requestOptions)
    .then(res => setQuestions(res.json()))
  }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65589434

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档