我有一个JsonObject名为"mapping"以下内容:
{
"client": "127.0.0.1",
"servers": [
"8.8.8.8",
"8.8.4.4",
"156.154.70.1",
"156.154.71.1"
]
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以得到数组"servers":
mapping.get("servers").getAsJsonArray()
Run Code Online (Sandbox Code Playgroud)
现在我想解析JsonArray成java.util.List...
最简单的方法是什么?
如何使用gson 2.2.4对这样的简单枚举进行序列化和反序列化?
public enum Color {
RED, BLUE, YELLOW;
}
Run Code Online (Sandbox Code Playgroud) @Override
public String toString() {
return new Gson().toJson(this);
}
Run Code Online (Sandbox Code Playgroud)
我是否通过简单地将此作为我的模型对象的默认行为来打破一些好的做法,"Joshua" -模式,一般设计模式或其他约定?
toString()无论如何只会在我们当前使用的范例(Android)中用于调试.这也是我喜欢在JSON中看到对象的原因,因为很多ORM/json持久性将通过http-> php/python-> mysql和本地SQLite发生.
如何在单击Cmd+Alt+L重新格式化代码时告诉PhpStorm 8.0.3保留以下数组:
$array = [
'short' => 1, // I want this...
'veryVeryVeryIncrediblyLong' => 123456789,
];
Run Code Online (Sandbox Code Playgroud)
而不是这样做:
$array = [
'short' => 1, // Not this...
'veryVeryVeryIncrediblyLong' => 123456789,
];
Run Code Online (Sandbox Code Playgroud)
我不希望它被对齐,因为即使一般来说它非常好,有时它也不起作用 - 例如在巨大的配置数组中,或者只是为了避免长时间的差异,如果你必须重新对齐一个完整的添加新密钥后的数组...
我有以下JSON,我只对获取元素感兴趣"status","lat"并且"lng".
使用Gson,是否可以解析此JSON以获取这些值而无需创建表示JSON内容的整个类结构?
JSON:
{
"result": {
"geometry": {
"location": {
"lat": 45.80355369999999,
"lng": 15.9363229
}
}
},
"status": "OK"
}
Run Code Online (Sandbox Code Playgroud) 我正在研究一些服务器代码,客户端以JSON的形式发送请求.我的问题是,有许多可能的请求,所有请求都在小的实现细节中有所不同.因此我想使用Request接口,定义如下:
public interface Request {
Response process ( );
}
Run Code Online (Sandbox Code Playgroud)
从那里,我在一个名为LoginRequest如下所示的类中实现了接口:
public class LoginRequest implements Request {
private String type = "LOGIN";
private String username;
private String password;
public LoginRequest(String username, String password) {
this.username = username;
this.password = password;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return …Run Code Online (Sandbox Code Playgroud) 我想知道我如何能够选择"最终" Exception,其中包含一条详细信息,其中包含许多链接异常的所有详细信息.
例如,假设这样的代码:
try {
try {
try {
try {
//Some error here
} catch (Exception e) {
throw new Exception("FIRST EXCEPTION", e);
}
} catch (Exception e) {
throw new Exception("SECOND EXCEPTION", e);
}
} catch (Exception e) {
throw new Exception("THIRD EXCEPTION", e);
}
} catch (Exception e) {
String allMessages = //all the messages
throw new Exception(allMessages, e);
}
Run Code Online (Sandbox Code Playgroud)
我对完整不感兴趣stackTrace,但只是在我写的信息中.我的意思是,我想得到这样的结果:
java.lang.Exception: THIRD EXCEPTION + SECOND EXCEPTION + FIRST EXCEPTION
Run Code Online (Sandbox Code Playgroud) 所以我正在尝试做的是检索列表中第一个项目的索引,该索引以"what"开头,我不知道如何做到这一点.
我的尝试(笑):
List<string> txtLines = new List<string>();
//Fill a List<string> with the lines from the txt file.
foreach(string str in File.ReadAllLines(fileName)) {
txtLines.Add(str);
}
//Insert the line you want to add last under the tag 'item1'.
int index = 1;
index = txtLines.IndexOf(npcID);
Run Code Online (Sandbox Code Playgroud)
是的,我知道这不是什么,它是错的,因为它似乎正在寻找一个等于npcID的项目而不是以它开头的行.
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println(request.getParameter("msg").toString());
String data = request.getParameter("msg").toString();
Gson gson = new Gson();
MessageBase msggg = gson.fromJson(data, MessageBase.class);
//System.out.println(msggg.Id + msggg.MessageText);
}
Run Code Online (Sandbox Code Playgroud)
public abstract class MessageBase implements Serializable {
public int Id;
public String MessageText;
public Date ReceiveDate;
}
public class SyncSmsMessage extends MessageBase {
public String SenderNum;
}
Run Code Online (Sandbox Code Playgroud)
代码一直有效MessageBase msggg=gson.fromJson(data, MessageBase.class);.我得到这个例外:
java.lang.RuntimeException: Failed to invoke public com.example.syncapp.MessageBase() with no args
at com.google.gson.internal.ConstructorConstructor$2.construct(ConstructorConstructor.java:94)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:162)
at com.google.gson.Gson.fromJson(Gson.java:795)
at com.google.gson.Gson.fromJson(Gson.java:761)
at com.google.gson.Gson.fromJson(Gson.java:710) …Run Code Online (Sandbox Code Playgroud) 我无法通过这个,已经多次重建存储库,使ssh键完成
--------------
demo_app <username>$ git remote add origin git@github.com:<username>/demo_app.git
fatal: remote origin already exists.
$ git push -u origin master
---
ssh: Could not resolve hostname git: nodename nor servname provided, or not known
---
fatal: Could not read from remote repository.
---
Please make sure you have the correct access rights
---
and the repository exists.
----------
---------
Checked ssh keys
---
$ ssh -T git@github.com
Hi <username>! You've successfully authenticated, but GitHub does not provide shell access.
--- …Run Code Online (Sandbox Code Playgroud)