我正在使用Gson使用以下代码从给定的API反序列化JSON字符串.
Gson gson = new Gson();
Map<String, CustomDto> test = gson.fromJson(result, new TypeToken<Map<String, CustomDto>>() {}.getType());
Run Code Online (Sandbox Code Playgroud)
这CustomDto是一个由基元(int,long,boolean)和另一个Object构造的对象.我遇到的问题是这个Object是可选的.有时它会被传播,有时它不存在.我期待如果JSON字符串中缺少一个字段,不应该调用相关的set方法(比如杰克逊),它应该只是工作不幸的情况并非如此,我遇到了异常:
Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 207 column 23
Run Code Online (Sandbox Code Playgroud)
如果我从中删除该字段CustomDto,它只是工作正常但是如果它将被传输则存在问题.请问是否有一些注释标记实体类中的可选字段,或者有人可以给我一些建议如何处理这个?
感谢大家.
在詹姆斯·伊里的博文中,他写道:
另外,Scala有一个"选项"方法,可以将值提升为Some(value)或None,具体取决于它是否为null ...
我似乎无法option在scaladoc中的任何地方找到此方法.
Iulian Dragos的gdata客户端项目包含一个可能是James所指的方法.
def option[A <: AnyRef](a: A): Option[A] =
if (a eq null) None else Some(a)
Run Code Online (Sandbox Code Playgroud)
请指出在scaladoc中我可以在哪里找到此方法.
PS我有一个看起来像这样的方法:
def permutations(s: String): List[String] = ...
Run Code Online (Sandbox Code Playgroud)
我有两个想法,我是否应该改为:
def permutations(s: Option[String]): List[String] = ...
Run Code Online (Sandbox Code Playgroud)
因为客户端可以调用它null.目前在第一个实例中,我希望使用option前面提到的方法手动将String参数&I框化.
我想搬家UIPopoverController.我找到的唯一方法是使用视图中的新位置调用[popover presentPopoverFromRect ...].
这实际上是移动它,但不是我想要的地方,我正在使用UIPopoverArrowDirectionAny.
还有其他选择吗?
我想知道是否有一个简短的手来折叠选项的地图.例如
def divideByThree(x:Int) = if (x == 0) None else Some(x/3)
val result = Some(6) map (divideByThree(_))
resut:Option[Option[Int]] = Some(Some(2))
Run Code Online (Sandbox Code Playgroud)
为了解决这个问题我做了
val result = Some(6) match {
case Some(i) => divideByThree(i)
case None => None
}
Run Code Online (Sandbox Code Playgroud)
这看起来有点沉重.我可以在Option上创建一个隐式函数map mapOption来解决这个问题,但我想知道是否有一种我没有想过的更好的方法.
如何在Scala中我可以在主构造函数中定义局部变量?
我需要从Scala for the impatient书中解决这个练习:
编写一个具有主构造函数的Person,该构造函数接受包含第一个名称,空格和姓氏的字符串,例如new Person("Fred Smith").提供只读属性firstName和lastName.主构造函数参数应该是var,val还是plain参数?为什么?
现在我的解决方案看起来像这样:
class Person(firstLast: String) {
private[this] val firstLastAsArr = firstLast.trim.split(" ")
val firstName = firstLastAsArr (0)
val lastName = firstLastAsArr (1)
}
Run Code Online (Sandbox Code Playgroud)
如何限制firstLastAsArr主要构造函数范围的变量可见性(现在它有类范围)?
我正在尝试将复杂的Java对象转换为JSON.但我无法这样做.我正在使用Java泛型.
这是存储页面输出的通用对象.我没有发布JsonTest对象结构.它与任何带有property和setter和getter的普通java类相同.
class ListRow<T>{
private int total;
private int currentRow;
private List<T> test;
public ListRow(int total, int currentRow, List<T> test) {
this.total = total;
this.currentRow = currentRow;
this.test = test;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getCurrentRow() {
return currentRow;
}
public void setCurrentRow(int currentRow) {
this.currentRow = currentRow;
}
public List<T> getTest() {
return test;
}
public void setTest(List<T> test) {
this.test = test;
}
JsonTest …Run Code Online (Sandbox Code Playgroud) 我知道 Some 对象可以是 None 或我传递的对象之一。鉴于它不是 None 的事实,从 Some 对象中提取字段的理想方法是什么?我制作了一个“At”类,它的字段之一是“日期”。由于 Some 类具有与 Product 特征的混合,因此以下工作正常:
(An object with return type Some(At)).productElement(0).asInstanceOf[At].date
Run Code Online (Sandbox Code Playgroud)
但是有没有一种理想的方法来做到这一点?
我是Gson解析的新手,并且做了很少的基本Gson解析.但是这次我的JSON非常复杂.我的JSON看起来像:
{"uname":"man101",
"uid":"2",
"account":{
"entry":[8,15.48],
"exit":[8,15.48],
"details":
[[0,0],[0,8.2],[1.15,8.2],[1.15,18.23],[7.33,18.23],[7.33,15.48],[12.15,2.28],
[12.35,2.28],[12.35,0],[10.65,0],[10.65,1.42],[8.1,1.42],[8.1,3.95],
[4.25,3.95],[4.25,0]],
"section":
[
{
"account":[[0,0],[0,3.35],
[4.25,3.35],[4.25,0]],
"category":"office",
"description":"Mobile based company",
"sectionname":"xyz",
"id":1
},
{
"account":[[0,3.95],[0,7.8],
[4.25,7.8],4.25,3.95]],
"category":"office",
"description":"Network based company",
"sectionname":"ABC",
"id":2
},
]
},
"category":"Cowork",
"description":"Combined office space"
}
Run Code Online (Sandbox Code Playgroud)
我试图通过以下方式解析它
public class AccountData
{
public String uname;
public String uid;
public String category;
public String description;
public Account account;
public class Account
{
public float[] entry;
public float[] exit;
public List<float[]> details;
public List<Section> section;
}
public class Section
{ …Run Code Online (Sandbox Code Playgroud) 我正在尝试做一些似乎应该在scala中具有直接语法/功能的东西.我只是不知道那是什么.我正在尝试将Option的包含值(如果它不是None)转换为另一种类型.
如果我想实现以下功能,我想知道代码是什么
def myFunc(inVal:Option[Double]):Option[BigDecimal] = {
//Code I need goes here
}
Run Code Online (Sandbox Code Playgroud)
注意:我实际上并没有实现这个功能,它只是我演示我需要的最清晰的方式