Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在不知道值格式的情况下使用GSON解析JSON_Java_Android_Json_Gson - Fatal编程技术网

Java 在不知道值格式的情况下使用GSON解析JSON

Java 在不知道值格式的情况下使用GSON解析JSON,java,android,json,gson,Java,Android,Json,Gson,大家好,我有一个api,就像你们可以看到的,我有一个元数据数组,数组中的所有项都有一个整数id和一个字符串键,但它们的值都不相同。我在对象中定义了值,但我有一个错误。这就是错误 Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 3871 path $[0].meta_data 这是我的POJO课程 主要Pojo(产品)类别 这是MetaDatum类 public class MetaDatum implements Parce

大家好,我有一个api,就像你们可以看到的,我有一个元数据数组,数组中的所有项都有一个整数id和一个字符串键,但它们的值都不相同。我在对象中定义了值,但我有一个错误。这就是错误

Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 3871 path $[0].meta_data
这是我的POJO课程

主要Pojo(产品)类别

这是MetaDatum类

public class MetaDatum implements Parcelable {

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("key")
@Expose
private String key;
@SerializedName("value")
@Expose
private Object value;

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getKey() {
    return key;
}

public void setKey(String key) {
    this.key = key;
}

public Object getValue() {
    return value;
}

public void setValue(Object value) {
    this.value = value;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeValue(this.id);
    dest.writeString(this.key);
    dest.writeParcelable((Parcelable) this.value, flags);
}

public MetaDatum() {
}

protected MetaDatum(Parcel in) {
    this.id = (Integer) in.readValue(Integer.class.getClassLoader());
    this.key = in.readString();
    this.value = in.readParcelable(Object.class.getClassLoader());
}

public static final Parcelable.Creator<MetaDatum> CREATOR = new Parcelable.Creator<MetaDatum>() {
    @Override
    public MetaDatum createFromParcel(Parcel source) {
        return new MetaDatum(source);
    }

    @Override
    public MetaDatum[] newArray(int size) {
        return new MetaDatum[size];
    }
};
}
public类MetaDatum实现可包裹{
@序列化名称(“id”)
@暴露
私有整数id;
@序列化名称(“键”)
@暴露
私钥;
@序列化名称(“值”)
@暴露
私人客体价值;
公共整数getId(){
返回id;
}
公共无效集合id(整数id){
this.id=id;
}
公共字符串getKey(){
返回键;
}
公共无效设置键(字符串键){
this.key=key;
}
公共对象getValue(){
返回值;
}
公共无效设置值(对象值){
这个值=值;
}
@凌驾
公共int描述内容(){
返回0;
}
@凌驾
公共无效写入包裹(包裹目的地,内部标志){
dest.writeValue(此.id);
dest.writeString(此键);
dest.writeparceable((可包裹)this.value,标志);
}
公共元数据(){
}
受保护的元基准面(地块中){
this.id=(整数)in.readValue(Integer.class.getClassLoader());
this.key=in.readString();
this.value=in.readParcelable(Object.class.getClassLoader());
}
public static final Parcelable.Creator=新建Parcelable.Creator(){
@凌驾
公共元基准createFromParcel(地块源){
返回新的元基准面(源);
}
@凌驾
公共元数据[]新数组(整数大小){
返回新的元基准面[大小];
}
};
}

您的元数据是数组而不是对象

meta_data": [
      {
        "id": 3281,
        "key": "_vc_post_settings",
        "value": {
          "vc_grid_id": []
        }
      ]
所以使用列表

private List<MetaDatum> metaData;
私有列表元数据;

使用robopojo puligns或

之后,让改造对象定义基本url和其他内容

public class ApiClient {
private final static String BASE_URL = "https://goorab.com/wp-json/wc/v2/";

public static ApiClient apiClient;
private Retrofit retrofit = null;

public static ApiClient getInstance() {
    if (apiClient == null) {
        apiClient = new ApiClient();
    }
    return apiClient;
}

//private static Retrofit storeRetrofit = null;

public Retrofit getClient() {
    return getClient(null);
}


private Retrofit getClient(final Context context) {

    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient.Builder client = new OkHttpClient.Builder();
    client.readTimeout(60, TimeUnit.SECONDS);
    client.writeTimeout(60, TimeUnit.SECONDS);
    client.connectTimeout(60, TimeUnit.SECONDS);
    client.addInterceptor(interceptor);
    client.addInterceptor(new Interceptor() {
        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {
            Request request = chain.request();

            return chain.proceed(request);
        }
    });

    retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .client(client.build())
            .addConverterFactory(GsonConverterFactory.create())
            .build();


    return retrofit;
}
}
并为api调用制作接口

public interface ApiInterface {
@GET("products")
Call<ResponseData> getdata(@Query("consumer_key") String key);
公共接口{
@获取(“产品”)
调用getdata(@Query(“consumer_key”)字符串键);
}

然后调用下面的活动或片段

ApiInterface apiInterface = ApiClient.getInstance().getClient().create(ApiInterface.class);
    Call<ResponseData> responseCall = apiInterface.getdata("pass key");
    responseCall.enqueue(new Callback<ResponseData>() {
        @Override
        public void onResponse(Call<ResponseData> call, retrofit2.Response<ResponseData> response) {
            if (response.isSuccessful() && response.body() != null && response != null) {
                Toast.makeText(getApplicationContext(), "GetData" + response.body().getLanguage(), Toast.LENGTH_SHORT).show();


            }
        }

        @Override
        public void onFailure(Call<ResponseData> call, Throwable t) {
            Log.d("Errror", t.getMessage());
        }
    });
ApiInterface-ApiInterface=ApiClient.getInstance().getClient().create(ApiInterface.class);
调用responseCall=apinterface.getdata(“传递键”);
responseCall.enqueue(新的回调(){
@凌驾
公共void onResponse(呼叫,改装2.响应){
if(response.issusccessful()&&response.body()!=null&&response!=null){
Toast.makeText(getApplicationContext(),“GetData”+response.body().getLanguage(),Toast.LENGTH_SHORT).show();
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.d(“Errror”,t.getMessage());
}
});

并确保pojo类都有效。

除必须转义的字符外,所有Unicode字符都可以放在引号内:引号、反向索利多卡因和控制字符(U+0000到U+001F)

添加您的url以调用api并提供json数据,然后我提供code.pl提供完整链接以提供json数据响应。@AndroidTeam hi非常感谢您的帮助这是我获取数据的url:我以前尝试过这个pojo生成器站点,但它不起作用。我有相同的错误使用robopojo pulign用于pojo类生成。您的Unicode文本破坏数据中的JSON格式,因此您必须使用编码的JSON数据其编码您可以发送请求:是的..我看到,名称属性内容文本需要编码整个JSON文件没有问题我可以接收文件我的问题是元数据,我看不到元数据上的名称
ApiInterface apiInterface = ApiClient.getInstance().getClient().create(ApiInterface.class);
    Call<ResponseData> responseCall = apiInterface.getdata("pass key");
    responseCall.enqueue(new Callback<ResponseData>() {
        @Override
        public void onResponse(Call<ResponseData> call, retrofit2.Response<ResponseData> response) {
            if (response.isSuccessful() && response.body() != null && response != null) {
                Toast.makeText(getApplicationContext(), "GetData" + response.body().getLanguage(), Toast.LENGTH_SHORT).show();


            }
        }

        @Override
        public void onFailure(Call<ResponseData> call, Throwable t) {
            Log.d("Errror", t.getMessage());
        }
    });