0

Android Scala アプリでケース クラス グラフを JSON にシリアル化しています (を使用"com.hanhuy.sbt" % "android-sdk-plugin" % "1.3.5")。

を使用し"org.json4s" %% "json4s-native" % "3.2.10"ていますが、次のような単純なケース クラスでも失敗します。

package test
case class Test(text: String)

実際にシリアル化するコードは次のようになります。

import org.json4s._
import org.json4s.native.Serialization
import org.json4s.native.Serialization.{read, write}

// ...

implicit val formats = Serialization.formats(NoTypeHints)

val test = Test("test")
val serialized = write(test)
info(s"Serialized to '$serialized'")

出力は次のとおりです。

Serialized to '{}'

ProGuard の問題が疑われ、ProGuard の設定はbuild.sbt次のようになっています。

proguardScala in Android := true

proguardOptions in Android ++= Seq(
  "-dontobfuscate",
  "-dontoptimize",
  "-keepattributes Signature",
  "-dontwarn scala.collection.**", // required from Scala 2.11.3
  "-dontwarn scala.collection.mutable.**", // required from Scala 2.11.0
  "-ignorewarnings",
  "-keep class scala.Dynamic",
  "-keep class test.**"
)

私も試してみましjson4s-jacksonたが、違いはありませんでした。

ProGuard ログは次のようになります。

Warning: com.thoughtworks.paranamer.AnnotationParanamer$Jsr330Helper: can't find referenced class javax.inject.Named
Warning: com.thoughtworks.paranamer.AnnotationParanamer$Jsr330Helper: can't find referenced class javax.inject.Named
Warning: org.joda.convert.JDKStringConverter$9: can't find referenced class javax.xml.bind.DatatypeConverter
Warning: org.joda.convert.JDKStringConverter$9: can't find referenced class javax.xml.bind.DatatypeConverter
Warning: org.joda.convert.JDKStringConverter$9: can't find referenced class javax.xml.bind.DatatypeConverter
Note: com.google.android.gms.internal.av calls '(com.google.ads.mediation.MediationAdapter)Class.forName(variable).newInstance()'
Note: com.google.android.gms.maps.internal.q: can't find dynamically referenced class com.google.android.gms.maps.internal.CreatorImpl
Note: org.joda.time.DateTimeZone calls '(org.joda.time.tz.Provider)Class.forName(variable).newInstance()'
Note: org.joda.time.DateTimeZone calls '(org.joda.time.tz.NameProvider)Class.forName(variable).newInstance()'
Note: there were 1 unresolved dynamic references to classes or interfaces.
  You should check if you need to specify additional program jars.
  (http://proguard.sourceforge.net/manual/troubleshooting.html#dynamicalclass)
Note: there were 3 class casts of dynamically created class instances.
  You might consider explicitly keeping the mentioned classes and/or
  their implementations (using '-keep').
  (http://proguard.sourceforge.net/manual/troubleshooting.html#dynamicalclasscast)
Warning: there were 5 unresolved references to classes or interfaces.
     You may need to add missing library jars or update their versions.
     If your code works fine without the missing classes, you can suppress
     the warnings with '-dontwarn' options.
     (http://proguard.sourceforge.net/manual/troubleshooting.html#unresolvedclass)
Note: You're ignoring all warnings!

何か案は?

4

1 に答える 1

-1

コンストラクタを使用して String から JSONObject を作成できます。

 JSONObject json = new JSONObject(myString);

JSONObject を String に変換するには、toString() メソッドを使用します。

 String myString = json.toString();

さらに、JSONObject から特定の文字列値を取得しようとしている場合は、次のようにすることができます。

 if (json.has("content"))
{
    String content = json.getString("content");
    //do something with content string
}

最後に、JSONObject の使用に慣れていない場合は、droidQuery が提供する次のような解析ツールを使用することをお勧めします。

Object[] array = $.toArray(myJSONArray);

Map<String, ?> map = $.map(myJSONObject);
于 2014-11-13T07:58:22.730 に答える