Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
182 views
in Technique[技术] by (71.8m points)

Deserialize ElasticSearch json object in Angular

I have this class in Angular

export class Stock {
    public companyName: string;
    public codeUrl: string;
}

I have a service that does a query to ElasticSearch. It is very basic at the moment:

export class SentimentAnalysisService {

  elasticCodeUrl = 'localhost:5000/stock/_search';

  constructor(private httpClient: HttpClient) { }

  public getStocks(stockName: string): Observable<Stock[]> {
    return this.httpClient.get<Stock[]>(this.elasticCodeUrl + stockName);
  }

}

This method return a json that has this structure:

{
"took":3,
"timed_out":false,
"_shards":{
   "total":1,
   "successful":1,
   "skipped":0,
   "failed":0
},
"hits":{
   "total":{
      "value":7,
      "relation":"eq"
   },
   "max_score":1.09601,
   "hits":[
      {
         "_index":"stock_ia",
         "_type":"stock",
         "_id":"d7fuzXYByNJeri0tMEYH",
         "_score":1.09601,
         "_source":{
            "stockName":"Apple Inc",
            "symbole":"AAPL",
            "urlCode":"APPLE-INC-4849"
         }
       }, 
      {
         "_index": "stock_ia",
         "_type": "stock",
         "_id": "ZLfuzXYByNJeri0tMEYB",
         "_score": 0.93801093,
         "_source": {
             "stockName": "Baidu Inc ADR",
             "symbole": "BIDU",
             "urlCode": "BAIDU-INC-8563"
            }
        }
   ]
}
}

I can't find a way to deserialize this to get back my array of "Stock". I could do a get("hits.hits") but then I'm stuck. How can I deserialize this json into an array of "Stock"?

question from:https://stackoverflow.com/questions/65909384/deserialize-elasticsearch-json-object-in-angular

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...