Domanda

Ho due oggetti che mi piacerebbe raggruppare e spedire in un array, codificato come JSON con Mochijson.Sono:

> Book0 = {struct, [{"title", "a book"}, {"id", "1"}]}.      
> Book1 = {struct, [{"title", "another book"}, {"id", "2"}]}.
.

Tuttavia,

> mochijson:encode({struct, [{"books", [Book0, Book1]}]}).
** exception exit: {json_encode,{bad_char,{struct,[{"title","a book"},
                                                   {"id","1"}]}}}
     in function  mochijson:json_encode_string_unicode_1/1 (src/mochijson.erl, line 203)
     in call from mochijson:json_encode_string_unicode/1 (src/mochijson.erl, line 190)
     in call from mochijson:'-json_encode_proplist/2-fun-0-'/3 (src/mochijson.erl, line 151)
     in call from lists:foldl/3 (lists.erl, line 1197)
     in call from mochijson:json_encode_proplist/2 (src/mochijson.erl, line 154)
.

Come posso strutturare i miei dati per Mochijson?Sono consapevole di questo Articolo maPurchly, non copre gli array di oggetti.

È stato utile?

Soluzione

.

mochijson: codifica ({struct, [{"libri", { array , [book0, book1]}}]}).

[123,"\"books\"",58,
 [91,
  [123,"\"title\"",58,"\"a book\"",44,"\"id\"",58,"\"1\"",125],
  44,
  [123,"\"title\"",58,"\"another book\"",44,"\"id\"",58,
   "\"2\"",125],
  93],
 125]
.

Ma il consenso generale sembra effettivamente essere: "Usa Mochijson2" (che funzionerebbe correttamente per entrambi i casi con e senza array).

Altri suggerimenti

La migliore risposta, dopo un po 'di immersione di origine, è: preferire Mochijson2 a meno che tu non abbia un motivo eccellente non.

Mochijson2 è anche più veloce di Mochijson.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top