如何在 android 中设置两次 json 对象?

how to set twice json object in android?

我试过,但它给我错误

我的JSON:

{
    "status":true,
    "message":"Front About Found",
    "data":{
        "FrontAbout":{
            "ABOUT_ID":"1",
            "CONTENT":"and the arts. ",
            "SCHOOL_NAME":"The New School",
            "ADDRESS":" Western Corridor",
            "LOGO":"14643339731.png"
        }
    }
}

我的示例代码:

{
    JSONArray jsonarray = jObj.getJSONArray("data");

    for (int z = 0; z < jsonarray.length(); z++) {

        JSONObject jobj = jsonarray.getJSONObject(z);

        JSONObject jobj_FrontAbout = jobj.getJSONObject("FrontAbout");

        school = jobj_FrontAbout.getString("SCHOOL_NAME");
        add = jobj_FrontAbout.getString("ADDRESS");
        phone = jobj_FrontAbout.getString("CONTACT_NO");
        email = jobj_FrontAbout.getString("EMAIL");
        cont = jobj_FrontAbout.getString("CONTENT");
        image = jobj_FrontAbout.getString("LOGO");

        setData();

        loadingView.dismiss();

    }

    loadingView.dismiss();
    if (loadingView != null && loadingView.isShowing()) {
        loadingView.dismiss();
    }
}

它给我错误“JSON对象无法转换为JSON数组”

我想在 textview 中而不是在数组中进行简单设置 请给我解决方案

试试这个:

JSONObject jsonObj= jObj.getJSONObject("your json string");
JSONObject jsonInnerObj= jsonObj.getJSONObject("data");

你可以这样解析。

JSONObject jobj = jsonarray.getJSONObject(z);
status=jobj.getBoolean("status");
message=jobj.getString("message");
                        JSONObject dataObject = jobj.getJSONObject("data");
                        JSONObject frontAboutObject = dataObject.getJSONObject("FrontAbout");

                        school = frontAboutObject.getString("SCHOOL_NAME");
                        add = frontAboutObject.getString("ADDRESS");
                        phone = frontAboutObject.getString("CONTACT_NO");
                        email = frontAboutObject.getString("EMAIL");
                        cont = frontAboutObject.getString("CONTENT");
                        image = frontAboutObject.getString("LOGO");

虽然 "data" 是一个 JSONObJect,但您正在将 "data" 作为 JSOnArray 进行解析。

JSON 节点将以方括号或大括号开头。 [ 和 { 之间的区别是,方括号 ([) 表示 JSONArray 节点的开始,而大括号 ({) 表示 JSONObject.

{
JSONObject dataJsonObject = jObj.getJSONObject("data");

for (int z = 0; z < dataJsonObject.length(); z++) {

    JSONObject jobj = jsonarray.getJSONObject(z);

    JSONObject jobj_FrontAbout = jobj.getJSONObject("FrontAbout");

    school = jobj_FrontAbout.getString("SCHOOL_NAME");
    add = jobj_FrontAbout.getString("ADDRESS");
    phone = jobj_FrontAbout.getString("CONTACT_NO");
    email = jobj_FrontAbout.getString("EMAIL");
    cont = jobj_FrontAbout.getString("CONTENT");
    image = jobj_FrontAbout.getString("LOGO");

    setData();

    loadingView.dismiss();

}

loadingView.dismiss();
if (loadingView != null && loadingView.isShowing()) {
    loadingView.dismiss();
}

}

HopeFully 你会摆脱你的错误。干杯