【问题标题】:Retrofit JSON Array Parsing改造 JSON 数组解析
【发布时间】:2016-03-16 16:35:43
【问题描述】:

无法理解如何解析这个 json 数组。


[{"id":"154","ontime":"2016-03-15","offtime":"2016-04-14","usluga_id":"2","name":" 50mpbs","period":"30","cost":"300"},

{"id":"152","ontime":"2016-03-15","offtime":"2016-04-14","usluga_id":"4","name":"TV60 ","期间":"30","成本":"100"},

{"id":"156","ontime":"2016-03-15","offtime":"2016-04-04","usluga_id":"5","name":"TV120 ","期间":"20","成本":"200"},

{"id":"153","ontime":"2016-03-15","offtime":"2016-03-30","usluga_id":"6","name":"SMS -check","period":"15","cost":"50"}]


@Generated("org.jsonschema2pojo")

public class UserService {
@SerializedName("id")
@Expose
private String id;
@SerializedName("ontime")
@Expose
private String ontime;
@SerializedName("offtime")
@Expose
private String offtime;
@SerializedName("usluga_id")
@Expose
private String uslugaId;
@SerializedName("name")
@Expose
private String name;
@SerializedName("period")
@Expose
private String period;
@SerializedName("cost")
@Expose
private String cost;

/**
 *
 * @return
 * The id
 */
public String getId() {
    return id;
}

/**
 *
 * @param id
 * The id
 */
public void setId(String id) {
    this.id = id;
}

/**
 *
 * @return
 * The ontime
 */
public String getOntime() {
    return ontime;
}

/**
 *
 * @param ontime
 * The ontime
 */
public void setOntime(String ontime) {
    this.ontime = ontime;
}

/**
 *
 * @return
 * The offtime
 */
public String getOfftime() {
    return offtime;
}

/**
 *
 * @param offtime
 * The offtime
 */
public void setOfftime(String offtime) {
    this.offtime = offtime;
}

/**
 *
 * @return
 * The uslugaId
 */
public String getUslugaId() {
    return uslugaId;
}

/**
 *
 * @param uslugaId
 * The usluga_id
 */
public void setUslugaId(String uslugaId) {
    this.uslugaId = uslugaId;
}

/**
 *
 * @return
 * The name
 */
public String getName() {
    return name;
}

/**
 *
 * @param name
 * The name
 */
public void setName(String name) {
    this.name = name;
}

/**
 *
 * @return
 * The period
 */
public String getPeriod() {
    return period;
}

/**
 *
 * @param period
 * The period
 */
public void setPeriod(String period) {
    this.period = period;
}

/**
 *
 * @return
 * The cost
 */
public String getCost() {
    return cost;
}

/**
 *
 * @param cost
 * The cost
 */
public void setCost(String cost) {
    this.cost = cost;
}

public class ServicesArray {

private List<UserService> services;
public ServicesArray(){};

public List<UserService> getServices() {
    return services;
}

public void setServices(List<UserService> services) {
    this.services = services;
}

活动中...

public void GetUserServices(){

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://cybergenesis.ru/egor/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    APIService service = retrofit.create(APIService.class);

    Call<ServicesArray> call = service.getUserServices("egoraa");

    call.enqueue(new Callback<ServicesArray>() {
        @Override
        public void onResponse(Call<ServicesArray> call, retrofit2.Response<ServicesArray> response) {
            String[] services = new String[response.body().getServices().size()];
            response.body().getServices().toArray(services);
        Log.d(TAG,"OKKKK");
        }

        @Override
        public void onFailure(Call<ServicesArray> call, Throwable t) {
            Log.d(TAG,"ERROR PROFIT");
        }
    });
}

日志:D/MainScreen:错误利润


响应为空,我的问题是什么?

【问题讨论】:

    标签: java json gson retrofit2


    【解决方案1】:

    您发布的 JSON 是一个用户列表,因此您应该将其反序列化为 User 对象列表,而不是创建一个 ServicesArray 类来解析:

      Type type = new TypeToken<Collection<User>>(){}.getType();
      List<User> users = new Gson().fromJson(jsonString,type);
    

    代替:

      List<User> users = new Gson().fromJson(jsonString,User.class);
    

    【讨论】:

      【解决方案2】:

      您有一组用户。将此用作服务回调的返回类型。数组映射到改造中的列表。

      public class UserList{
      
      private List<User>users;
      
      public UserList(){};
      
      public List<User>getUsers() {
      return users;
      }
      
       public void setUsers(List<User>users) {
        this.users= users;
       }
      } 
      

      【讨论】:

        【解决方案3】:

        如果所有响应都是 json 格式,您可以在所有响应上设置 GSON 转换器

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

        【讨论】:

          猜你喜欢
          • 2016-04-27
          • 1970-01-01
          • 2016-12-19
          • 2017-05-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-09-04
          • 2021-09-06
          相关资源
          最近更新 更多