Question

J'ai deux objets que j'aimerais regrouper et expédier dans un tableau, codés en JSON avec mochijson.Ils sont:

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

Cependant,

> 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)

Comment structurer mes données pour Mochijson ?J'en suis conscient article mais malheureusement, cela ne couvre pas les tableaux d’objets.

Était-ce utile?

La solution

mochijson:encode({struct, [{"livres", {tableau, [Livre0, Livre1]}}]}).

[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]

Mais le consensus général semble effectivement être le suivant :"utilisez mochijson2" (ce qui fonctionnerait correctement dans les deux cas avec et sans array).

Autres conseils

La meilleure réponse, après un peu de plongée source, est la suivante: préférez MOCHIJSON2 à moins que vous n'ayez une excellente raison de ne pas.

Mochijson2 est également plus rapide que Mochijson.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top