我有一个数组,我正在进行一个服务调用,我想将所有返回的json数组设置为我的,这是到目前为止的代码:
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":“品牌满意度”}}
发布于 2021-01-06 02:40:36
你可以这样做。
const [questions, setQuestions] = useState<QuestionHeader[]>([]);
function runService() {
const requestOptions = {
method: 'GET',
headers: { 'Content-Type': 'application/json' }
};
fetch("URL", requestOptions)
.then(res => setQuestions(res.json()))
}https://stackoverflow.com/questions/65589434
复制相似问题