无法反序列化对象-预期为BEGIN_OBJECT,但在第1行第1列处为STRING [英] Cant deserialize object - Expected BEGIN_OBJECT but was STRING at line 1 column 1

查看:105
本文介绍了无法反序列化对象-预期为BEGIN_OBJECT,但在第1行第1列处为STRING的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我要说的是,我在发布之前已经搜索过,只是找不到答案.

First let me say, I have searched before posting, just cant find the answer.

我在反序列化JSON时遇到麻烦.它是有效的JSON(在 http://jsonlint.com/中选中) 它是使用servicestack json序列化程序生成的.

I'm having trouble de-serializing a JSON. It is a valid JSON (checked at http://jsonlint.com/) and it was produced with servicestack json serializer.

我要

java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1

这是服务器发送的我的JSON:

Here is my JSON sent by the server:

{
    "artistName": "",
    "pathInfo": "C:\\Users\\Unknown\\Desktop\\Audio Sketches\\Chill.mp3",
    "indexPos": 0,
    "messageType": "song"
}

接收方式:

{"artistName":"","pathInfo":"C:\\Users\\Unknown\\Desktop\\Audio Sketches\\Chill.mp3","indexPos":0,"messageType":"song"}

这里是持有它的对象:

public class Song {

    private String artistName;
    private String albumName;
    private String titleName;
    private String pathInfo;
    private String indexPos;
    private String messageType;


    public Song() {
    }

推荐答案

好,我最终自己找到了答案,但要感谢所有支持和帮助.

Okay I end up finding the answer myself but thanks to all that supported and helped.

这是接收JSON对象并使用GSON初始化反序列化的代码.

This is the code that received the JSON object and initiate the Deserialization using GSON.

 Map messageObjMap = new Gson().fromJson(message, Map.class);
 String type = messageObjMap.get("messageType").toString();

 switch (type) {
     case "song":
         try {
             Gson gson = new GsonBuilder().create();
             Song song = gson.fromJson(message, Song.class);
             ...
             ...
         } ...
     ...
 }

消息"被假定为整个JSON.相反,它只是从JSON解析的唯一密钥,这就是为什么反序列化不起作用的原因.希望能对某人有所帮助

"message" was suppose to be the entire JSON. Instead it was a only a key parsed from the JSON and this is why the deserializtion didn't work. Hope that will help someone

这篇关于无法反序列化对象-预期为BEGIN_OBJECT,但在第1行第1列处为STRING的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆