반작용에서 가져오기까지 가는 모든 서버에 데이터 분석에서 예상치 못한 JSON 형식 번호

0

질문

내가 생성한 웹 응용 프로그램을 사용하는 이동을 서버에 데이터를 제공하게 반응하는 앱입니다.

의 건축에서 나 server,나를 따라 제공하는 튜토리얼에 의해 이동 문서(https://golang.org/doc/tutorial/web-service-gin). 내 코드와 매우 유사하는 코드,그리고 내가 예상 결과를 얻을 사용하면 .

[
  {
    "id":0,
    "date":"2021-11-21T12:00:00Z",
    "rentedTo":"Bob",
    "rentedBy":"Jim"
  },
  //etc...

내가 사용하는 경우 반응의 가 API,그러나,나의 결과에 다시 와서 예기치 않은 형태입니다.

fetch('http://localhost:8080/rentals')
.then((response)=> {
  if (response.ok) {
    return response.json();
  }
  throw response;
.then((jsonArr) => {
  handleRentalsData(jsonArr)
  })
.catch((error) => {
  console.error("Error fetching data: ", error);
});

을 때 나 console.log(jsonArr)브라우저에 보고서를 다음과 같다:

Array(3) [ {...},{...},{...} ]
//Expanding this object via the console shows the following information:
---> 0: Object { id: 0, date: "2021-11-21T12:00:00Z", rentedTo: "Bob", ... } 
---> 1: Object { id: 1, date: "2021-11-22T12:00:00Z", rentedTo: "Bob", ... }
---> 2: Object { id: 2, date: "2021-11-23T12:00:00Z", rentedTo: "Bob", ... }

이러한 지표(그리고 브라우저 상표)표시된 데이터를 형태로 지금의 배열을,그래서 처리했습니다.

내가 하려고 했을 통해 루프 이 배열을 분석하 json 문자열의 내부는 그지만, JSON.parse(data) 생산 숫자만(0,1,2 각각)보다는 생체로,이메일 주소와 비밀번호를 입력해.

for (const json in jsonArr){
  console.log(typeof json);            //string
  const rentalObj = JSON.parse(json);
  console.log(typeof rentalObj);       //number
  console.log(rentalObj);              //0, 1, and 2 respectively
  console.log(rentalObj.date);         //undefined, because rentalObj is now a number.
}

나는 몇 가지를 찾고,들어 있는 인덱스는 배열의 데이터에 와서 일으킬 수 있는 문제,그래서 나는 시도하는 분석과 함께 자극성능,뿐만 아니라.

for (const json in jsonArr){
  console.log(typeof json);            //string
  const rentalObj = JSON.parse(json, (key, value) => {
    console.log(key);                  //<empty string>
    console.log(value);                //0, 1, and 2
    return(value);
  });
  console.log(typeof rentalObj);       //number
  console.log(rentalObj);              //0, 1, and 2 respectively
  console.log(rentalObj.date);         //undefined
}

JSON.parse(jsonArr) 오류가 발생으로 예상된다. 나에 부딪쳤습니다. 왜 분석으로(n index)번호? 어떻게 추출할 수 있는 객체의 내부에 배열할 때,구문 분석(또는 인쇄)문자열의 내부에 배열을 생산 숫자만?

fetch-api go javascript json
2021-11-23 04:47:53
1

최고의 응답

1

jsonfor (const json in jsonArr) { 로 설정됩니다"열거 속성의" jsonArr 배,이 경우에는 인덱스입니다.

for (const x in ['foo','bar','baz']) { console.log(x) };
// output:
// 0
// 1
// 2

그래서 당신은 아마 사용하고 싶지 않은 for ... in. 대신 사용할 수 있는 for ... of 반복 배열의 요소:

for (const x of ['foo','bar','baz']) { console.log(x) };
// output:
// foo
// bar
// baz

배열을 반복하고 for ... in:

참고: for...in 사용할 수 없습을 반복하는 배열에 인덱스 순서는 중요합니다.

배열 색인은 열거 속성으로 정수 이름 그렇지 않으면과 동일한 일반 객체 속성입니다. 이 없 는 것을 보장 for...in 을 반환하는 인덱스에서 어떤 특정한 다. 이 for...in 루프에서 반환 모두 열거 속성을 포함하여,그들 비수 정수 이름과 그 상속됩니다.

기 때문에 주의 반복은 구현에 따라 다릅니다 반복하고 이상 배열하지 않을 수 있습 방문의 요소에 일관성의 순서. 따라서, 그것을 사용하는 것이 좋습니다 for 루프와 숫자 인덱스(또는 Array.prototype.forEach()for...of 루프)를 반복할 때 이상 배열의 안정성을 더욱 높이기 위해 액세스가 중요합니다.

2021-11-23 05:47:08

다른 언어로

이 페이지는 다른 언어로되어 있습니다

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................