[FIXED] Wie filtert man JSON-Dateien in der Jinja-Vorlage?

Ausgabe

Ich war frustriert, als ich JSON-Dateien filterte, ich bin neu in JSON-Kenntnissen.

aber ich habe einige JSON-Dateien aus der URL importiert, der Code sieht so aus:

{
   "id":"_n2tEUURGiY",
   "title":"I Just",
   "formats":[
      {
         "format_id":"sb2",
         "format_note":"storyboard",
         "ext":"mhtml",
         "protocol":"mhtml",
         "acodec":"none",
         "vcodec":"none",
         "url":"",
         "width":48,
         "height":27,
         "fps":0.45871559633027525,
         "rows":10,
         "columns":10,
         "fragments":[
            {
               "url":"",
               "duration":218.0
            }
         ],
         "audio_ext":"none",
         "video_ext":"none",
         "format":"sb2 - 48x27 (storyboard)",
},
      {
         "format_id":"sb1",
         "format_note":"storyboard",
         "ext":"mhtml",
         "protocol":"mhtml",
         "acodec":"none",
         "vcodec":"none",
         "url":"",
         "width":45,
         "height":45,
         "fps":0.5091743119266054,
         "rows":10,
         "columns":10,
         "fragments":[
            {
               "url":"",
               "duration":196.39639639639637
            },
            {
               "url":"",
               "duration":21.60360360360363
            }
         ],
         "audio_ext":"none",
         "video_ext":"none",
         "format":"sb1 - 45x45 (storyboard)",
         "resolution":"45x45",
         "http_headers":{
            "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4556.0 Safari/537.36",
            "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
            "Accept-Language":"en-us,en;q=0.5",
            "Sec-Fetch-Mode":"navigate"
         }
      },
{
         "asr":48000,
         "filesize":3772787,
         "format_id":"251",
         "format_note":"medium",
         "source_preference":-1,
         "fps":"None",
         "audio_channels":2,
         "height":"None",
         "quality":3,
         "has_drm":false,
         "tbr":138.348,
         "url":"",
         "width":"None",
         "language":"",
         "language_preference":-1,
         "preference":"None",
         "ext":"webm",
         "vcodec":"none",
         "acodec":"opus",
         "dynamic_range":"None",
         "abr":138.348,
         "downloader_options":{
            "http_chunk_size":10485760
         },
         "container":"webm_dash",
         "protocol":"https",
         "audio_ext":"webm",
         "video_ext":"none",
         "format":"251 - audio only (medium)",
         "resolution":"audio only",
         "http_headers":{
            "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4556.0 Safari/537.36",
            "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
            "Accept-Language":"en-us,en;q=0.5",
            "Sec-Fetch-Mode":"navigate"
         }
      },
{
         "asr":44100,
         "filesize":"None",
         "format_id":"18",
         "format_note":"360p",
         "source_preference":-1,
         "fps":25,
         "audio_channels":2,
         "height":360,
         "quality":6,
         "has_drm":false,
         "tbr":172.717,
         "url":",
         "width":360,
         "language":"",
         "language_preference":-1,
         "preference":"None",
         "ext":"mp4",
         "vcodec":"avc1.42001E",
         "acodec":"mp4a.40.2",
         "dynamic_range":"SDR",
         "protocol":"https",
         "video_ext":"mp4",
         "audio_ext":"none",
         "vbr":172.717,
         "abr":0.0,
         "format":"18 - 360x360 (360p)",
         "resolution":"360x360",
         "filesize_approx":4819495,
         "http_headers":{
            "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4556.0 Safari/537.36",
            "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
            "Accept-Language":"en-us,en;q=0.5",
            "Sec-Fetch-Mode":"navigate"
         }
      },

und dann füge ich iteration (use {% for format in formats %}) in eine andere Datei wie diese ein:

{% for format in formats %}   
                        
         {{format.format_name}}                          
                        
                    
 {% endfor %}

Leider wird bei dieser Iteration das gesamte generierte Format angezeigt.

meine frage ist, wie kann ich filtern, nur anzeigen nach 'format_id'ist “sb2” und 'format_id'ist “sb1”> ?

Vielen Dank

EDIT: Wie kommt es, dass ich oben in diesem Thread nicht HI sagen kann? es wird immer automatisch entfernt

Lösung

Innerhalb Ihrer for-Schleife können Sie eine if-Anweisung jinja if verwenden

{% for format in formats %}   
  {% if format.format_id in ["sb1", "sb2"] %}
         {{format.format_name}}                          
  {% endif %}                    
{% endfor %}

PS: In Ihren JSON-Daten haben die Formatobjekte keinen “format_name”-Schlüssel.


Beantwortet von –
ubaumann


Antwort geprüft von –
Mildred Charles (FixError Admin)

0 Shares:
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like

[FIXED] String mit Array vergleichen

Ausgabe Ich möchte eine Zeichenfolge (Postleitzahl, zB: “50000”) mit einem Array von Zeichenfolgenelementen vergleichen, und wenn das Array…