Cómo iterar sobre la lista JSON y seleccionar un elemento

Pregunta:

Digamos que tengo una lista de nombres con identificadores, en JSON:

(let ((json-object-type 'plist))
  (setq mylist (json-read-from-string "[{\"name\": \"Adam\", \"id\": \"1\"},
                                        {\"name\": \"Eve\", \"id\": \"2\"}]")))

Quiero averiguar la identificación de Adam. ¿Cómo puedo hacer eso?

Estoy intentando actualmente

(dolist (person  mylist)
  (when (equal "Adam" (plist-get person :name))
    (setq person_id (plist-get person :id)))
  )

Pero esto genera setq: argumento de tipo incorrecto: listp, [(: id "1": nombre "Adam") (: id "2": nombre "Eva")]

Respuesta:

Puede instruir a json-read-from-string para analizar matrices JSON como listas elisp al permitir vincular json-array-type a la lista como tal

(let ((json-object-type 'plist)
      (json-array-type 'list))
  (setq mylist (json-read-from-string "[{\"name\": \"Adam\", \"id\": \"1\"},
                                        {\"name\": \"Eve\", \"id\": \"2\"}]")))                                 {\"name\": \"Eve\", \"id\": \"2\"}]")))

mylist ahora sería una lista elisp que manipularía / atravesaría usando funciones de lista existentes.

Leave a Comment

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

web tasarım