Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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
Android 无法从JSON数组分析两个不同的对象_Android_Json - Fatal编程技术网

Android 无法从JSON数组分析两个不同的对象

Android 无法从JSON数组分析两个不同的对象,android,json,Android,Json,我试图从JSON数组中获取两个不同的对象,但不知何故,我无法获取第二个对象。不确定我是否正确解析了它。我还想从open_小时中获取元素open_,并将其设置为method SetAuthor。以下是JSON数组的代码url: 首先,开放时间是一个JSONObject而不是JSONArray 第二件事是结果数组中的打开时间对象 所以解析应该是这样的 for (int i = 0; i < jarray.length(); i++) { JSONObject object = jar

我试图从JSON数组中获取两个不同的对象,但不知何故,我无法获取第二个对象。不确定我是否正确解析了它。我还想从open_小时中获取元素open_,并将其设置为method SetAuthor。以下是JSON数组的代码url:


首先,开放时间是一个JSONObject而不是JSONArray

第二件事是结果数组中的打开时间对象

所以解析应该是这样的

for (int i = 0; i < jarray.length(); i++) {
     JSONObject object = jarray.getJSONObject(i);

     Elements parameters = new Elements();

     parameters.setTitle(object.getString("name"));
     parameters.setImage(object.getString("icon"));

     elementList.add(parameters);

     // add here
     JSONObject jarraytwo = jsono.getJSONObject("opening_hours");
}

试试这个JSON解析

                JSONObject jsono = new JSONObject(data);
                JSONArray jarray = jsono.getJSONArray("results");

                for (int i = 0; i < jarray.length(); i++) {
                    JSONObject object = jarray.getJSONObject(i);

                    Elements parameters = new Elements();

                    parameters.setTitle(object.getString("name"));
                    parameters.setImage(object.getString("icon"));

                    elementList.add(parameters);


                JSONArray jarraytwo = object.getJSONArray("opening_hours");

                for (int i = 0; i < jarraytwo.length(); i++) {
                    JSONObject objecttwo = jarraytwo.getJSONObject(i);

                    Elements parameterstwo = new Elements();

                    parameterstwo.setAuthor(objecttwo.getString("open_now"));

                    Toast.makeText(getApplicationContext(), "open_now : " + objecttwo.getString("open_now"), Toast.LENGTH_LONG).show();

                    elementListtwo.add(parameterstwo);
                }
                } 

开放时间是对象,而不是数组。将json添加到post中,而不是共享api密钥!。我建议你阅读关于改装和GSON/Jackson.Hi@Kunu的文章,谢谢你的回复,你能发布完整的代码吗?我还没有完全明白。谢谢
public class Elements {

    private String title;
    private String description;
    private String author;
    private String publishedat;
    private String urltoimage;

    public Elements() {
        // TODO Auto-generated constructor stub
    }

    public Elements(String title, String description, String author,
                    String publishedat, String urltoimage) {

        this.title = title;
        this.description = description;
        this.author = author;
        this.publishedat = publishedat;
        this.urltoimage = urltoimage;
    }


    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getPublishedat() {
        return publishedat;
    }

    public void setPublishedat(String publishedat) {
        this.publishedat = publishedat;
    }

    public String getImage() {
        return urltoimage;
    }

    public void setImage(String urltoimage) {
        this.urltoimage = urltoimage;
    }

}
for (int i = 0; i < jarray.length(); i++) {
     JSONObject object = jarray.getJSONObject(i);

     Elements parameters = new Elements();

     parameters.setTitle(object.getString("name"));
     parameters.setImage(object.getString("icon"));

     elementList.add(parameters);

     // add here
     JSONObject jarraytwo = jsono.getJSONObject("opening_hours");
}
                JSONObject jsono = new JSONObject(data);
                JSONArray jarray = jsono.getJSONArray("results");

                for (int i = 0; i < jarray.length(); i++) {
                    JSONObject object = jarray.getJSONObject(i);

                    Elements parameters = new Elements();

                    parameters.setTitle(object.getString("name"));
                    parameters.setImage(object.getString("icon"));

                    elementList.add(parameters);


                JSONArray jarraytwo = object.getJSONArray("opening_hours");

                for (int i = 0; i < jarraytwo.length(); i++) {
                    JSONObject objecttwo = jarraytwo.getJSONObject(i);

                    Elements parameterstwo = new Elements();

                    parameterstwo.setAuthor(objecttwo.getString("open_now"));

                    Toast.makeText(getApplicationContext(), "open_now : " + objecttwo.getString("open_now"), Toast.LENGTH_LONG).show();

                    elementListtwo.add(parameterstwo);
                }
                }