从JSON中提取字节数组 [英] Extracting a byte array from a JSON

查看:102
本文介绍了从JSON中提取字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下格式的JSON:

  [{fingerprint:[79,0,0, 0,18,0,0,0,18,0,0,0,19,0,0,0,23,0,0,0,40,0,0,0,42,0,0,0, 63,0,0,0,68,0,0,0,71,0,....]}] 

我一直试图从它中提取字节数组,使用以下代码:

  JSONFingerprint fingerprintData = gson .fromJson(obj,JSONFingerprint.class); 

其中JSONFingerprint是:

  public class JSONFingerprint {

byte []指纹;

public byte [] getfingerprintData(){
返回指纹;
}

}

我得到这个错误:

 线程Thread-0中的异常com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期的BEGIN_ARRAY,但是STRING 
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory $ Adapter.read(ReflectiveTypeAdapterFactory.java:176)
at com.google.gson.Gson.fromJson(Gson.java:795)
在com.google.gson.Gson.fromJson(Gson.java:859)
在com.google.gson.Gson.fromJson(Gson.java:832)

有人有什么想法吗?

解决方案

看起来您的 JSON POJO 不匹配。可能有两种可能的解决方案。



解决方案1: -



如果你不能改变JSON格式(即)你的JSON就是这样,

  [{fingerprint:[79 ,0,0,0,18,0,0,0,18,0,0,0,19,0,0,0,23,0,0,0,40,0,0,0,42,0 ,0,0,63,0,0,0,68,0,0,0,71,0,....]}] 

然后,您需要将 JSONFingerprint 类更改为: -

  public class JSONFingerprint {

字符串指纹;

public String getfingerprintData(){
return fingerprint;


$ / code $ / pre

你需要解析 JSON 像这样: -

  JSONFingerprint [] dummy = new JSONFingerprint [0]; 
JSONFingerprint [] fingerPrint = gson.fromJson(json,dummy.getClass());
System.out.println(fingerPrint [0] .getfingerprintData());

解决方案2:

如果你的POJO不能改变(即)你的POJO就是这样,

  public class JSONFingerprint {

byte []指纹;

public byte [] getfingerprintData(){
返回指纹;




$ b $ p $然后你必须改变你的<$ c

$ p $ {code> {fingerprint:[1, 2,3,4,5,6,7,8,9,10]}

你可以像通常那样解析它: - $ / $>

  JSONFingerprint fingerprintData = gson.fromJson(obj,JSONFingerprint.class) ; 

希望这可以帮到你!


I have a JSON of the following format:

[{"fingerprint":"[79,0,0,0,18,0,0,0,18,0,0,0,19,0,0,0,23,0,0,0,40,0,0,0,42,0,0,0,63,0,0,0,68,0,0,0,71,0,....]"}]

I have been trying to extract the byte array from it using the following:

JSONFingerprint fingerprintData = gson.fromJson(obj, JSONFingerprint.class);

Where JSONFingerprint is:

public class JSONFingerprint {

    byte[] fingerprint;

    public byte[] getfingerprintData() {
        return fingerprint;
    }

}

I have been getting this error:

Exception in thread "Thread-0" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
    at com.google.gson.Gson.fromJson(Gson.java:795)
    at com.google.gson.Gson.fromJson(Gson.java:859)
    at com.google.gson.Gson.fromJson(Gson.java:832)

Does anybody have any ideas??

解决方案

It seems your JSON and POJO don't match. There can be 2 possible solutions for this.

Solution 1:-

If you cannot change the JSON format(i.e.)your JSON is gonna be this,

[{"fingerprint":"[79,0,0,0,18,0,0,0,18,0,0,0,19,0,0,0,23,0,0,0,40,0,0,0,42,0,0,0,63,0,0,0,68,0,0,0,71,0,....]"}]

Then you need to change your JSONFingerprint class to this:-

public class JSONFingerprint {

    String fingerprint;

    public String getfingerprintData() {
        return fingerprint;
    }
}

and you need to parse your JSON like this:-

JSONFingerprint[] dummy = new JSONFingerprint[0]; 
JSONFingerprint[] fingerPrint = gson.fromJson(json, dummy.getClass());
System.out.println(fingerPrint[0].getfingerprintData());

Solution 2:-

If your POJO cannot change(i.e.)your POJO is gonna be this,

public class JSONFingerprint {

    byte[] fingerprint;

    public byte[] getfingerprintData() {
        return fingerprint;
    }
}

Then you'll have to change your JSON format to something like this:-

{"fingerprint":[1,2,3,4,5,6,7,8,9,10]}

which you can parse it usually, as you've already done:-

JSONFingerprint fingerprintData = gson.fromJson(obj, JSONFingerprint.class);

Hope this helps you!

这篇关于从JSON中提取字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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