Как проанализировать данные JSON из SQLite3 для использования в приложении angular?

#json #angular #sqlite

#json #angular #sqlite

Вопрос:

Я извлек данные из SQLite3 как объект JSON, чтобы я мог получать результаты в формате JSON, который также включает массивы. Мой запрос SQLite3 выглядит следующим образом:

 let sql =  `SELECT json_object('id', id, 'company_id', company_id, 'title', title, 'link', link, 'domain', domain,
    'tags', 
    (SELECT json_group_array(json_object('tag_id', t.id, 'corporate_statement_link_id', t.corporate_statement_link_id, 'name', t.name)) 
    FROM tags as t
    LEFT JOIN corporate_statements c
    on c.corporate_statement_link_id = s.id
    WHERE t.corporate_statement_link_id = c.corporate_statement_link_id )) AS statement
    FROM search_results as s
    WHERE s.company_id = ?`

    db.all(sql, [id], (err, row) => {
        if(err){
            res.status(500).send(err);
        }
        console.log("Row: ", row);
        res.send(row);
    });
 

Этот результат был возвращен как объект JSON с помощью express router. Однако возвращаемые данные представлены в следующем формате:

 [
  {
    "statement": "{"id":1,"company_id":1,"title":"Making a Difference in Racial Equity","link":"https://corporate.walmart.com/newsroom/2020/06/05/making-a-difference-in-racial-equity","domain":"corporate.walmart.com","tags":[{"tag_id":4,"corporate_statement_link_id":1,"name":"box"},{"tag_id":2,"corporate_statement_link_id":1,"name":"ceo"},{"tag_id":3,"corporate_statement_link_id":1,"name":"letter"},{"tag_id":1,"corporate_statement_link_id":1,"name":"statement"},{"tag_id":5,"corporate_statement_link_id":1,"name":"test"},{"tag_id":6,"corporate_statement_link_id":1,"name":"test2"},{"tag_id":7,"corporate_statement_link_id":1,"name":"test3"}]}"
  },
  {
    "statement": "{"id":2,"company_id":1,"title":"Something Happened in Our Town: A Child's Story about Racial ...","link":"https://www.walmart.com/ip/Something-Happened-in-Our-Town-A-Child-s-Story-about-Racial-Injustice-Hardcover-9781433828546/718225519","domain":"www.walmart.com","tags":[]}"
  },
  {
    "statement": "{"id":3,"company_id":1,"title":"Advancing Our Work on Racial Equity","link":"https://corporate.walmart.com/newsroom/2020/06/12/advancing-our-work-on-racial-equity","domain":"corporate.walmart.com","tags":[]}"
  },
  {
    "statement": "{"id":4,"company_id":1,"title":"Injustice : Exposing the Racial Agenda of the Obama Justice ...","link":"https://www.walmart.com/ip/Injustice-Exposing-the-Racial-Agenda-of-the-Obama-Justice-Department/15905044","domain":"www.walmart.com","tags":[]}"
  },
  {
    "statement": "{"id":5,"company_id":1,"title":"Making a Difference in Racial Equity: Walmart CEO Doug McMillon's ...","link":"https://corporate.walmart.com/equity","domain":"corporate.walmart.com","tags":[]}"
  },
  {
    "statement": "{"id":6,"company_id":1,"title":"Dog Whistle Politics : How Coded Racial Appeals Have Reinvented ...","link":"https://www.walmart.com/ip/Dog-Whistle-Politics-How-Coded-Racial-Appeals-Have-Reinvented-Racism-and-Wrecked-the-Middle-Class-Paperback-9780190229252/43190331","domain":"www.walmart.com","tags":[]}"
  },
  {
    "statement": "{"id":7,"company_id":1,"title":"Factbox: Corporations Pledge $1.7 Billion to Address Racism, Injustice","link":"https://www.usnews.com/news/top-news/articles/2020-06-09/factbox-corporations-pledge-17-billion-to-address-racism-injustice","domain":"www.usnews.com","tags":[]}"
  },
  {
    "statement": "{"id":8,"company_id":1,"title":"Applicative Justice : A Pragmatic Empirical Approach to Racial ...","link":"https://www.walmart.com/ip/Applicative-Justice-A-Pragmatic-Empirical-Approach-to-Racial-Injustice-9781442260009/47479777","domain":"www.walmart.com","tags":[]}"
  },
  {
    "statement": "{"id":9,"company_id":1,"title":"Walmart facing criticism over 'All Lives Matter,' 'Irish Lives Matter' T ...","link":"https://thehill.com/business-a-lobbying/504664-walmart-facing-criticism-over-sale-of-all-lives-matter-irish-lives-matter","domain":"thehill.com","tags":[]}"
  },
  {
    "statement": "{"id":10,"company_id":1,"title":"Topics Today: Racism and Racial Justice (Paperback) - Walmart.com","link":"https://www.walmart.com/ip/Topics-Today-Racism-and-Racial-Justice-Paperback/914848457","domain":"www.walmart.com","tags":[]}"
  }
]
 

При таком формате я не мог извлечь информацию из инструкции и проанализировать ее, чтобы затем использовать ее в angular.

 <mat-list-item *ngFor="let statement of statements" routerLink="/statement-detail" [queryParams]="{id: statement.id}" class="pointer">
    <p> {{statement.title}}</p>
    <p>
        <span *ngFor="let t of statement.tags">
            <span>t.name</span>
        </span>
    </p>
    <mat-divider *ngIf="!last"></mat-divider>

</mat-list-item>
 

Как я могу это решить?

Комментарии:

1. Этот запрос не выдаст этот json…

2. @Shawn, не могли бы вы уточнить? Я скопировал и вставил результат. Я не уверен, что вы имеете в виду