كيف يمكنني استخدام mochijson لترميز مجموعة من الكائنات؟

StackOverflow https://stackoverflow.com//questions/9610118

  •  09-12-2019
  •  | 
  •  

سؤال

لدي كائنين أرغب في تجميعهما وشحنهما في مصفوفة، مشفرتين كـ JSON مع mochijson.هم:

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

لكن،

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

كيف أقوم بتنظيم بياناتي لـ mochijson؟أنا على علم بهذا شرط لكنه، للأسف، لا يغطي صفائف من الكائنات.

هل كانت مفيدة؟

المحلول

mochijson:تشفير({struct, [{"كتب"، {مجموعة مصفوفة, ، [الكتاب0، الكتاب1]}}]}).

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

ولكن يبدو أن الإجماع العام هو:"استخدام mochijson2" (والذي سيعمل بشكل صحيح في كلتا الحالتين مع وبدون array).

نصائح أخرى

أفضل إجابة، بعد القليل من الغوص المصدر، هو: تفضل mochijson2 إلا إذا كان لديك سبب ممتاز لعدم.

mochijson2 أسرع أيضا ثم mochijson.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top