如何在POJO中使用动态键设置此JSON并在Android JAVA中从中获取数据?

时间:2018-12-25 07:50:46

标签: java android json

我正在开发一个电子商务应用程序,并且正在从API获取此JSON数据。

{“状态”:0,“数据”:{“商店”:{“ test-5”:{“名称”:“测试5”,“位置”:{“名称”:“某个地方B” ,“ id”:2},“ cover”:“ IMAGE-URL-HERE”},“ test-2”:{“ name”:“ Test 2”,“ locality”:{“ name”:“ Some place A “,” id“:2},” cover“:” IMAGE-URL-HERE“}}},” action“:[[” DATA“,” stores“]]}}

我也为此数据创建了一些POJO

private string CreateSqlFilter(string fieldName, Control userInputControl, SqlCommand command, bool exactMatch)
{
    string searchValue = null;
    if (userInputControl is TextBox) searchValue = ((TextBox)userInputControl).Text;
    if (userInputControl is ComboBox) searchValue = ((ComboBox)userInputControl).Text;
    if (String.IsNullOrWhiteSpace(searchValue)) return null;

    if (exactMatch)
    {
        command.Parameters.Add(new SqlParameter("@" + fieldName, searchValue));
        return fieldName + " = @" + fieldName;
    }
    else
    {
        command.Parameters.Add(new SqlParameter("@" + fieldName, "%" + searchValue + "%"));
        return fieldName + " LIKE @" + fieldName;
    }
}

private void button2_Click(object sender, EventArgs e)
{
    SqlCommand selectCommand = new SqlCommand();

    var filterConditions = new[] {
        CreateSqlFilter("Name_Arabic", txtName_Arabic, selectCommand, false),
        CreateSqlFilter("gender", CBgender, selectCommand, false),
        CreateSqlFilter("CIVILIDD", txtCIVILIDD, selectCommand, true),
        CreateSqlFilter("NATIONALITY", cbNationality, selectCommand, false)
        // etc.
    };

    string filterCondition = filterConditions.Any(a => a != null) ? filterConditions.Where(a => a != null).Aggregate((filter1, filter2) => String.Format("{0} AND {1}", filter1, filter2)) : (string)null;

    using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["myDatabase"].ConnectionString))
    {
        selectCommand.Connection = connection;
        selectCommand.CommandText = filterCondition == null ? "SELECT * FROM tabl1" : "SELECT * FROM tabl1 WHERE " + filterCondition;
        connection.Open();
        SqlDataAdapter adapter = new SqlDataAdapter(selectCommand);
        DataTable dataSource = new DataTable();
        adapter.Fill(dataSource);
        dataGridView1.DataSource = dataSource;
    }
}

// ------------- 公共类PartnerStoresPOJO {

public class PartnerStoreMainPOJO {
    @SerializedName("partnerstore")
    @Expose
    private PartnerStoresPOJO partnerstore;
    /**
     *
     * @return
     * The data
     */
    public PartnerStoresPOJO getPartnerStore() {
        return partnerstore;
    }
    /**
     *
     * @param partnerstore
     * The data
     */
    public void setPartnerStore(PartnerStoresPOJO partnerstore) {
        this.partnerstore = partnerstore;
    }
}

}  // ----------------

公共类PartnerStoreDetailPOJO {

@SerializedName("partnerstoredetail")
@Expose
private Map<String, PartnerStoreDetailPOJO> partnerstoredetail;

/**
 *
 * @return
 * The feeds
 */
public Map<String, PartnerStoreDetailPOJO>  getpartnerstoredetail() {
    return partnerstoredetail;
}
/**
 *
 * @param partnerstoredetail
 * The feeds
 */
public void setpartnerstoredetail(Map<String, PartnerStoreDetailPOJO> partnerstoredetail) {
    this.partnerstoredetail = partnerstoredetail;
}

}

// ----------------

@SerializedName("partnerstorelocality")
@Expose
private Map<String, PartnerStoreLocalityPOJO> partnerstorelocality;


@SerializedName("cover")
@Expose
private String cover;
@SerializedName("name")
@Expose
private String name;

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

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

/**
 * @return The cover
 */
public String getCover() {
    return cover;
}

/**
 * @param cover The address
 */
public void setCover(String cover) {
    this.cover = cover;
}

public Map<String, PartnerStoreLocalityPOJO> getpartnerstorelocality() {
    return partnerstorelocality;
}

public void setpartnerstorelocality(Map<String, PartnerStoreLocalityPOJO> partnerstorelocality) {
    this.partnerstorelocality = partnerstorelocality;
}

// ---------------

Amd我正在使用排球库。这是我的Java代码-

public class PartnerStoreLocalityPOJO {
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("id")
    @Expose
    private String id;
    /**
     *
     * @return
     * The name
     */
    public String getName() {
        return name;
    }
    /**
     *
     * @param name
     * The name
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     *
     * @return
     * The id
     */
    public String getId() {
        return id;
    }
    /**
     *
     * @param id
     * The id
     */
    public void setId(String id) {
        this.id = id;
    }
}

我正在使用该数据字符串。

1 个答案:

答案 0 :(得分:1)

尝试一次:进行相应的更改,它可以为您提供查询的方向。

public void convertJSON(JSONObject jsonObject) {
        try {
            JSONObject object = jsonObject.getJSONObject("data");

            Iterator<String> iter = object.keys();
            while (iter.hasNext()) {
                String key = iter.next();
                Object value = object.get(key);

                JSONObject obj2 = object.getJSONObject(key);
                //set key to POJO

                Iterator<String> iter2 = obj2.keys();
                while (iter2.hasNext()) {
                    String key2 = iter2.next();
                    //....so on
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }