Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android json4s将案例类测试(“Test”)序列化为;{};安卓上_Android_Json_Proguard_Json4s - Fatal编程技术网

Android json4s将案例类测试(“Test”)序列化为;{};安卓上

Android json4s将案例类测试(“Test”)序列化为;{};安卓上,android,json,proguard,json4s,Android,Json,Proguard,Json4s,我正在Android Scala应用程序中将一个案例类图序列化为JSON(使用“com.hanhuy.sbt”%”Android sdk插件“%”1.3.5“) 我使用的是“org.json4s”%%“json4s native”%%“3.2.10”,即使使用一个简单的case类,它也会失败,如下所示: package test case class Test(text: String) import org.json4s._ import org.json4s.native.Serializ

我正在Android Scala应用程序中将一个案例类图序列化为JSON(使用
“com.hanhuy.sbt”%”Android sdk插件“%”1.3.5“

我使用的是
“org.json4s”%%“json4s native”%%“3.2.10”
,即使使用一个简单的case类,它也会失败,如下所示:

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'")
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.**"
)
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!
实际序列化的代码如下所示:

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'")
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.**"
)
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!
输出为:

Serialized to '{}'
我怀疑有ProGuard问题,我在
build.sbt
中的ProGuard设置如下:

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'")
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.**"
)
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!
我也试过使用
json4s-jackson
,但没有什么不同

ProGuard日志如下所示:

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'")
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.**"
)
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!

有什么想法吗?

您可以使用构造函数从字符串创建JSONObject:

 JSONObject json = new JSONObject(myString);
要将JSONObject转换为字符串,只需使用toString()方法:

此外,如果您试图从JSONObject获取特定的字符串值,可以执行以下操作:

 if (json.has("content"))
{
    String content = json.getString("content");
    //do something with content string
}
最后,如果您不太习惯使用JSONObject,我建议您使用droidQuery提供的工具来帮助您进行解析,例如:

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

Map Map=$.Map(myJSONObject);

是的,我知道我可以使用多种替代JSON API。要求是坚持使用Scala惯用的东西,并且可以序列化/反序列化整个案例类图(就像json4s一样——只要它工作正常)。@JohnM,所以你想序列化一个特定的类到json4s,我想让json4s按照我的Android应用程序中的说明工作。要序列化的案例类图将非常重要。您能够解决此问题吗?我面临着完全相同的问题。不,我切换到了另一个序列化库(Spray Json)。谢谢。我最终使用了“argonaut”。spray json非常酷