在Android应用程序中,我需要为Kotlin数据类反序列化Json数据,只有一个抽象层。但是我没有任何想法,在构造函数中放置正确的属性。
作为一个简单的版本,假设我有一个形状:
abstract class Shape(open val x: Int, open val y: Int)
有两个派生
data class Circle(val radius: Int, override val x: Int, override val y: Int): Shape(x, y)
和
data class Square(val width: Int, override val x: Int, o
我正在尝试使用gson解析json数据。当我尝试使用GSON解析它时,我总是得到一个格式错误的JSON异常 com.google.gson.JsonSyntaxException:
com.google.gson.stream.MalformedJsonException: Expected ':' at line 1 column 8 path $.Meta
at com.google.gson.Gson.fromJson(Gson.java:947)
at com.google.gson.Gson.fromJson(Gson.java:8
我正在用Kotlin写一个应用程序。我有一个来自with服务的原始JSON字符串,我需要在Gson中使用它。
我正在做这个:
val gson = Gson()
val friends = gson.fromJson(response.rawResponse, JsonElement::class)
但是编译器无法找到当前可用的(fromJson(json: String!, typeOfT: Type!))正确的fromJson方法重载。
这就是错误:
Error:(65, 50) None of the following functions can be called with the
我正在使用GSON解析JSON数据。这是我的代码-
public class TestProg {
private static final String storeURL = "https://abcd"; //Haven't given the URL here completely
public static void main(String[] args) throws Exception {
String getData = getDataFromURL(storeURL);
Gson gson = new Gson();
如何使用gson.fromJson反序列化泛型类型数组
public class Cache<T> {
public void M(String json) {
JsonArray ja = (JsonArray) new JsonParser().parse(json);
Gson gson = new GsonBuilder().create();
T[] items = gson.fromJson(ja, ?);
}
}
这不起作用
Type t = new TypeToken<ArrayList<
我正在尝试将JSON字符串转换为HashMap。以下是我的一些代码:
public static void createSimpleAppointment(String json){
try
{
Gson gson = new Gson();
HashMap<String,String> data = new HashMap<String,String>();
data = gson.fromJson(json, data.getClass());
异常将抛出在这些行的最后一行(我在其中设置数据)。
JSON字符串是
{“身体”:“身体
先编码。
Gson gson = new Gson();
// 1.
ClassA a = gson.fromJson(json, ClassA.class)
gson.fromJson(jsonB, a)// make jsonB to a
// 2.
ClassB b = new ClassB();
b.setXXX(xxx);
b.setYYY(yyy);
......
gson.fromJson(json, b)// make json to b
这有可能吗?怎么做?
编辑
// JSON A
{
"name":"wener"
}
//
Json弦
{"userName":null," msgArr":[null],"numrow":0} //String result
葛森
new Gson().fromJson(result, MyClass.class);
MyClass
public String userName;
public int[] msgArr;
...
错误堆栈
at java.lang.IllegalArgumentException: Primitive array can't take null values.
at java.lang.ref
我正在尝试将JSON转换为自定义bean。但是,当JSON中的date值为null时,我将面临问题。
有效的JSON字符串,它正在被转换,不存在任何问题,因为from和迄今都有值:
{"title":"1201 Box Title 1","fromdate":"01/02/2017","description":"1201 Box Title 1","todate":"01/26/2017"}
带有错误的JSON字符串: Where from date为空
{"
在带有reified类型的函数中,我遇到了使用Gson反序列化的奇怪行为。只有当类型参数中涉及到interfaces时,才会发生这种情况。
请使用以下代码:
val toBeSerialized = listOf("1337")
with(Gson()) {
val ser = toJson(toBeSerialized)
val deser = fromJson<List<Serializable>>(ser)
}
第4行使用自定义扩展函数Gson.fromJson(json: String): T。
如果将定义为具体化,则失败:
in
我希望使用将json数据解析为java对象。
下面是我要解析的数据的一个示例:
{"isBean":{"userName":"test","password":"password112"}}
IsBean.java
public interface IsBean extends Serializable,IsSerializable{
long getId();
}
User.java
public class User implements IsBean,IsSerializable,Serial
我通过Bluetooth将字符串消息从.NET应用程序发送到Android Java应用程序。
使用Newtonsoft JSON.NET,我在JSON中序列化我的消息,并将它们按原样发送到Android应用程序,该应用程序试图使用Gson将它们转换回来。
这是我的消息类:
public final class Message {
private final String content;
private final String address;
private final long timeStamp;
private boolean isRead;
我想在java中使用gson来反序列化时间戳。 // simple class what I want to serialize and deserialize
public class TestTime {
private Timestamp time;
}
// and I create gson and try to deserialize it
String body = request.getReader().lines().collect(Collectors.joining());
TestTime data = gson.fromJson(bo
我需要将对象类传递给一个方法(gson.fromJson),但是JRuby说:'org.jruby.gen.InterfaceImpl624212141@187ef71' is not a Class
如何在Bob.class中引用类(例如JRuby )?
include Java
include_class Java::com.google.gson.Gson
include_class Java::com.bobmanager.Bob
def json_load(filename)
gson = Gson.new
jsonBob = IO.read filen
我有一个具有java.util.Date字段的Java对象。当我在Date对象上执行System.out.println()时,它输出"2012-11-03“。然后,当我在Java对象上执行gson.toJson()时,我可以看到Date字段的Json表示是"Nov 3,2012“。然后,当我尝试使用gson.fromJson()从Json字符串创建Java对象的另一个实例时,我得到了以下异常:
com.google.gson.JsonSyntaxException: Nov 3, 2012
at com.google.gson.internal.bind.DateTy
我使用GSON解析JSON的方式如下:
var obj = Gson().fromJson( json, Array<MyDataModel>::class.java ).toList()
我希望能够通过以下方法传递数据类型:
fun convert( t : Any ) : Any
{
return Gson().fromJson( mockedResponse, t ).toList()
}
val model = convert( Array<MyDataModel>::class.java )
但我明白:
None of the following f
我想以字符串格式存储一些数据,并使用android中的gson再次从该字符串中检索对象。
我的存储功能是
public void storeData(HashMap<String, List<String>> data) {
String d = new Gson().toJson(data);
storeToXYZ(d);
}
获取数据函数
public HashMap<String, List<String>> getData() {
String d = getFromXYZ();
// Assume not
我使用GSON将名为Profile的JPA实体从服务器传递给客户端
@Entity
public class Profile implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
.
.
.
}
GSON代码:
Gson gson = new Gson();
Profile p
我正在构建一个应用程序来学习如何用Kotlin解析JSON。在应用程序的第一阶段,我试图从JSON中提取图像并将它们粘贴到我的ImageView中,但不幸的是,我得到了以下错误:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Ada