我在我的应用程序中使用Gson,我有几个Java对象,我通过使用GSON将它转换为JSON来呈现给前端.我知道如何使用gson将JSON转换回Java对象.
我有这个用例:当我向GSON提供一个特定的JSON字符串时,我希望它找到从中获取JSON字符串的对象的类型,并给我一个该类型的对象.
使用GSON,只有在fromJson方法中传递Object类时才能返回对象.但无论如何我可以自己gson决定吗?我无法控制另一方生成的对象.
例:
Person person = new Person();
SomeRandomObject obj = new SomeRandomObject();
String personJson = gson.toJson(person);
String anotherJson = gson.toJson(obj);
Run Code Online (Sandbox Code Playgroud)
现在如果我想将它转换回对象,我使用这个:
Person p = gson.fromJson(personJson,Person.class);
Run Code Online (Sandbox Code Playgroud)
但无论如何,我可以得到这样的对象:
Object object = gson.fromJson(anotherJson,*TheCorrespondingObject.class*)
Run Code Online (Sandbox Code Playgroud)
我知道我可以通过尝试使用GSON抛出异常的不同对象的JSON字符串来实现这一点,但我不认为这是正确的方法.
我有一个界面
public interace ABC {
}
Run Code Online (Sandbox Code Playgroud)
其实现如下:
public class XYZ implements ABC {
private Map<String, String> mapValue;
public void setMapValue( Map<String, String> mapValue) {
this.mapValue = mapValue;
}
public Map<String, String> getMapValue() {
return this.mapValue
}
}
Run Code Online (Sandbox Code Playgroud)
我想使用实现为的 Gson 反序列化一个类
public class UVW {
ABC abcObject;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试反序列化它时,gson.fromJson(jsonString, UVW.class);它会返回给我null。jsonString 是 UTF_8 字符串。
是不是因为 UVW 类中使用的接口?如果是,我如何反序列化这样的类?
我上课了Dummy.我注入3个变量.但是,其中一个不能注射,因为它是一个界面.所以我注入一个对象,其中一个方法返回所需的类型.
Class Dummy
{
private final Class1 class1;
private final Class2 class2
private final Interface1 interface1;
@Inject
Dummy(Class1 class1, Class2 class2, HelperClass helperclass)
{
this.class1 = class1;
this.class2 = class2;
this.interface1 = helperclass.somefunction();
}
}
Run Code Online (Sandbox Code Playgroud)
该HelperClass的somefunction返回的一个实例Interface1.
这是我的测试:
@RunWith(MockitoJUnitRunner.class)
Class DummyTest
{
@Mock
private Class1 class1;
@Mock
private Class2 class2;
@Mock
private HelperClass helperclass;
@InjectMocks
private Dummy dummy;
@Before
public void start()
{
Interface1 mockInterface = mock(Interface1.class);
when(helperclass.somefunction()).thenReturn(mockInterface);
}
@Test
public void test() …Run Code Online (Sandbox Code Playgroud) Eclipse被设置为编译到'bin'的构建目录.我有一个包含各种Java .class文件(预编译)的文件夹结构,我想在编译时将其复制到文件夹'bin'中.
我如何告诉Eclipse自动执行此操作?
我一直在使用XML注释来记录我在VB.Net中的源代码(当我到达时,还有C#).我似乎记得很久以前有人告诉我,有一种方法可以自动将XML注释导出为文本文档或其他格式,以便在Visual Studio之外进行简单的引用.在那儿?它是什么,或者是它们?
有谁知道如何在j2me程序的源代码中隐藏密码内容?即所以人们不能将"DBT"看作是读取源代码的密码.
public void validateUser(String user, String Password) {
if (user.equals("N0203251") && Password.equals("DBT")) {
switchDisplayable(null, getContinue());
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用这个OAuth lib:http://github.com/jdg/oauthconsumer
将它添加到我的项目时,会导致超过2000个构建错误.这些错误都在"构建结果"窗口中的App_Preficx.pch文件下.
如果您实际上单击了错误,它们实际上包含在每个Foundation Header中.我不会列出所有这些,但这里有几个例子:
/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:236:0 /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:236: error: expected identifier or '(' before '@' token
/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:238:0 /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:238: error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token
/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:8:0 /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:8: error: expected identifier or '(' before '@' token
Run Code Online (Sandbox Code Playgroud)
您可以看到Xcode似乎无法链接到Foundation或至少无法导入标头.
如果我从Xcode中删除OAuth,一切都很好并编译.
我可以在我心中添加搜索路径,但无济于事.
我添加了文档中所需的所有框架,库和搜索路径.
在所有2641个错误中,没有一个能给我带来任何真正的问题.似乎也没有任何后果的警告.
有没有人知道如何找出问题的原因?
我已经为壁虎来源做了相当多的探索,但我似乎无法找到它.
当我这样做时,我发现通过firefox源代码散布了gecko这个词:
for i in `find .`; do grep -ibH gecko $i; done
Run Code Online (Sandbox Code Playgroud)
如果有人知道我可以在哪里获得源代码,我会非常感激.
在德国,我们有时会在域名中使用变元元音(变音符号).它的一部分internationalized domain name (IDN),如tile.öpnvkarte.de.
如果我尝试从那里下载Tile,那么java会抛出一个java.net.UnknownHostException: tile.öpnvkarte.de.
有人知道我怎么能解决这个问题吗?
我在处理我正在开发的一个更大程序的一个小组件时遇到了麻烦。基本上我需要让用户输入一个单词,并且需要打印第一个元音的索引。
word= raw_input("Enter word: ")
vowel= "aeiouAEIOU"
for index in word:
if index == vowel:
print index
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用。怎么了?