2016-07-04 1 views
1

この空のマップmapA = {}を宣言しましたが、何らかの理由でそれを解析済みのjson mapA = parsedJSONに設定すると、同じキーまたは重複するキーのエラーが発生します。空のマップを解析されたjson(新しいマップ)に正しく設定する方法はありますか?

私が取ったjsonは[{a:1},{a:2},{a:3}]のようなものです。明確にするために...これはjsonの権利ですか?それも配列の種類の原因ですか?

しかし、私はこれをjson文字列として取り出し、それを解析して、私が言及したエラーを受け取ります。

しかし、mapA = [{a:1},{a:2},{a:3}]は私にとっては機能しますが、これはハードコードされています。実際にここで何が間違っていますか?

EDIT1

ここで私はここで

[{"location_id":1,"flight_location":"KUL","description":"Kuala Lumpur","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":2,"flight_location":"KIX","description":"Osaka-Kansai","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":3,"flight_location":"PEK","description":"Beijing","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":4,"flight_location":"HGH","description":"Hangzhou","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":5,"flight_location":"PVG","description":"Shanghai","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":6,"flight_location":"SZX","description":"Shenzhen","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":7,"flight_location":"DPS","description":"Bali","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":8,"flight_location":"CGK","description":"Jakarta","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":9,"flight_location":"DMK","description":"Bangkok","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0}] 

を使用したサンプルデータは、私のコードです:

let fakeData = [{"location_id":1,"flight_location":"KUL","description":"Kuala Lumpur","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":2,"flight_location":"KIX","description":"Osaka-Kansai","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":3,"flight_location":"PEK","description":"Beijing","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":4,"flight_location":"HGH","description":"Hangzhou","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":5,"flight_location":"PVG","description":"Shanghai","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":6,"flight_location":"SZX","description":"Shenzhen","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":7,"flight_location":"DPS","description":"Bali","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":8,"flight_location":"CGK","description":"Jakarta","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0},{"location_id":9,"flight_location":"DMK","description":"Bangkok","update_by":null,"update_time":null,"create_by":null,"create_time":null,"rec_state":0}]; 

let flights = {}; 


fetch(link) //link is the api link which I use to fetch the data 
    .then(function(response) { 
     return response.json(); //In here I parse it 
    }).then(function(json) { 
     /*flights = fakeData;  Causes same error. Run this only to test when api is down */ 
     flights = json; // Same error. Set our flights map to the parsed json which is the map  
    }).catch(function(ex) { 
     console.log('parsing failed', ex); 
    }); 

コンソール出力:

enter image description here

これはReactライブラリからのものです。 Btw、私は本当にライブラリのコードに精通していない。

マイコンポーネント:

FlightList

const FlightList = ({flights, deleteFlight}) => { 
    return (
     <table className="table"> 
      <thead> 
       <tr> 
        <th>ID</th> 
        <th>Location</th> 
        <th>Description</th> 
       </tr> 
      </thead> 
      <tbody> 
       {flights.map(flight => 
        <FlightListRow key={flight.location_id} flight={flight}/> 
       )} 
      </tbody> 
     </table> 
    ); 
}; 

FlightListRow

const FlightListRow = ({flight}) => { 
    return (
     <tr> 
      <td><Link to={'/editflight/' + flight.location_id}>{flight.location_id}</Link></td> 
      <td><Link to={'/editflight/' + flight.location_id}>{flight.flight_location}</Link></td> 
      <td><Link to={'/editflight/' + flight.location_id}>{flight.description}</Link></td> 
     </tr> 
    ); 
}; 
+0

有効な 'JSON'は、' [{"a":1}、{"a":2}、{"a":3}] 'のような引用符でsorroundedというプロパティを持っています。したがって、これを解析することは失敗します。 JSONの有効性をチェックするには、次のようなウェブサイトを使用できます:http://json.parser.online.fr/ –

+0

@ ADreNaLiNe-DJ:重複するキーにどのように対処しますか? –

+0

私のjsonは有効です、私はちょうどチェックしました。だから私は 'mapA'にセットして、同じか重複しているキーエラーを取得しています。ここに私の結果があります:http://i.imgur.com/760Dy9w.png?1 –

答えて

1

これはエラーを反応させ、これはあなたのコンポーネントを反応させるには意味されて、あなたは、コンポーネントの配列を持っています2つ以上のコンポーネントで同じキーを使用しています

 {flights.map((flight,index) => 
      <FlightListRow key={index} flight={flight}/> 
     )} 
+0

私が使用するコンポーネントを追加しました。私のエラーはどの部分から来ていますか? –

+0

location_idが重複している必要があります。

+0

あなたの提案を試みました。 'index'が定義されていないというエラーが表示されます。 –

関連する問題