Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.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
Php 无法将JSON数组解析为对象_Php_Android_Json - Fatal编程技术网

Php 无法将JSON数组解析为对象

Php 无法将JSON数组解析为对象,php,android,json,Php,Android,Json,我搜索过这样的问题,但没有成功 我有JSON数据lke 首先是项目的词,最后是成功的词。数组中没有任何字符。当我删除它们时,我可以成功 StringBuilder sb = new StringBuilder(); sb.append(json+"\n");//Log.d(TAG, "sb "+sb.toString()); } try {Log.d(TAG, "xx "); JSONArray mJsonArray = new JSONArray(sb);Log.d(TAG,

我搜索过这样的问题,但没有成功

我有JSON数据lke

首先是项目的词,最后是成功的词。数组中没有任何字符。当我删除它们时,我可以成功

StringBuilder sb = new StringBuilder();

sb.append(json+"\n");//Log.d(TAG, "sb "+sb.toString());
}

try {Log.d(TAG, "xx ");
      JSONArray mJsonArray = new JSONArray(sb);Log.d(TAG, "xx x");
      JSONObject mJsonObject = new JSONObject();
这里它不能进入xx x日志,它进入错误捕获

但当我删除json格式的页眉和页脚时,它就起作用了。我应该使用字符串操作来删除这些或正则表达式吗

编辑:

我是这样解决的:

 try {Log.d(TAG, "xx ");
       JSONArray(sb);Log.d(TAG, "xx x");
                    JSONObject mJsonObject = new JSONObject();//i use this to get invidial value of json, do i need?

                    JSONObject jsono = new JSONObject(sb.toString());

                    Log.d(TAG,"jsono "+jsono.toString());

                    JSONArray items = jsono.getJSONArray("items");


                    Log.d(TAG,"items "+items);
                    Log.d(TAG,"items length "+items.length());

                           for (int i = 0; i < items.length(); i++) {


                           //   Log.d(TAG, "xx " +mJsonObject.getString("image"));
                               Log.d(TAG, "string: "+items.getJSONObject(i).getString("image"));//i need image urls
                               }
试试{Log.d(标记,“xx”);
JSONArray(sb);Log.d(标签“xx x”);
JSONObject mJsonObject=new JSONObject();//我用它来获取json的invidial值,需要吗?
JSONObject jsono=新的JSONObject(sb.toString());
d(标记“jsono”+jsono.toString());
JSONArray items=jsono.getJSONArray(“items”);
日志d(标签“项目”+项目);
Log.d(标记“items-length”+items.length());
对于(int i=0;i

这段代码完成了它的工作,但有时会出现try-catch错误,我看不到任何错误。有时需要很长的时间,大约1-2分钟,最终大部分都不会失败。为什么会这样?

整个响应是一个JSONObject,所以改为如下解析:

JSONObject jsono = new JSONObject(json);
您可以从中提取项目并获得成功:

JSONArray items = jsono.getJSONArray("items");
int success = jsono.getInt("success");

整个响应是一个JSONObject,因此改为如下解析:

JSONObject jsono = new JSONObject(json);
您可以从中提取项目并获得成功:

JSONArray items = jsono.getJSONArray("items");
int success = jsono.getInt("success");
使用此代码

Gson mGson=new Gson();
mGson.fromJson(jsonResponse.getJSONObject(i).toString(),dataChartDetailsQuiry.class);
使用此代码

Gson mGson=new Gson();
mGson.fromJson(jsonResponse.getJSONObject(i).toString(),dataChartDetailsQuiry.class);
试试这个 解析json的方法

public class Modelclass {

    String id;
    String name;
    String image;
    String number;
    String shape;
    String color;


    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getImage() {
        return image;
    }

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

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getShape() {
        return shape;
    }

    public void setShape(String shape) {
        this.shape = shape;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}
JSON解析代码

       ArrayList<Modelclass> arrayList = new ArrayList<>();
        try {
            JSONObject jsonObject = new JSONObject(jsonstring);
            JSONArray jsonArray = jsonObject.getJSONArray("items");
            Modelclass modelclass;
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject json_child_obje = jsonArray.getJSONObject(i);
                modelclass = new Modelclass();
                modelclass.setId(json_child_obje.getString("id"));
                modelclass.setId(json_child_obje.getString("name"));
                modelclass.setId(json_child_obje.getString("image"));
                modelclass.setId(json_child_obje.getString("number"));
                modelclass.setId(json_child_obje.getString("shape"));
                modelclass.setId(json_child_obje.getString("color"));
                arrayList.add(modelclass);
            }
            Log.d("ArraylistSize", "" + arrayList.size());
        } catch (JSONException e) {
            e.printStackTrace();
        }
ArrayList ArrayList=new ArrayList();
试一试{
JSONObject JSONObject=新的JSONObject(jsonstring);
JSONArray JSONArray=jsonObject.getJSONArray(“项”);
模型类模型类;
for(int i=0;i
试试这个 解析json的方法

public class Modelclass {

    String id;
    String name;
    String image;
    String number;
    String shape;
    String color;


    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getImage() {
        return image;
    }

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

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getShape() {
        return shape;
    }

    public void setShape(String shape) {
        this.shape = shape;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}
JSON解析代码

       ArrayList<Modelclass> arrayList = new ArrayList<>();
        try {
            JSONObject jsonObject = new JSONObject(jsonstring);
            JSONArray jsonArray = jsonObject.getJSONArray("items");
            Modelclass modelclass;
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject json_child_obje = jsonArray.getJSONObject(i);
                modelclass = new Modelclass();
                modelclass.setId(json_child_obje.getString("id"));
                modelclass.setId(json_child_obje.getString("name"));
                modelclass.setId(json_child_obje.getString("image"));
                modelclass.setId(json_child_obje.getString("number"));
                modelclass.setId(json_child_obje.getString("shape"));
                modelclass.setId(json_child_obje.getString("color"));
                arrayList.add(modelclass);
            }
            Log.d("ArraylistSize", "" + arrayList.size());
        } catch (JSONException e) {
            e.printStackTrace();
        }
ArrayList ArrayList=new ArrayList();
试一试{
JSONObject JSONObject=新的JSONObject(jsonstring);
JSONArray JSONArray=jsonObject.getJSONArray(“项”);
模型类模型类;
for(int i=0;i
首先创建arraylist来存储数据

ArrayList<List_items> list_items_arr = new ArrayList<List_items>();
从服务器获取响应并解析它

JSONObject jsono = new JSONObject(json);

JSONArray items = jsono.getJSONArray("items");

  for(int i=0;i<items.length();i++){
         JSONObject list_obj=items.getJSONObject(i);
            List_items list_items = new List_items();
            list_items.setItem_name(list_obj.getString("id"));
            list_items.setItem_sku(list_obj.getString("name"));
            list_items.setItem_id(list_obj.getString("image"));
     list_items_arr .add(list_items);
 }
JSONObject jsono=newjsonobject(json);
JSONArray items=jsono.getJSONArray(“items”);

对于(int i=0;i首先创建arraylist以存储yout数据

ArrayList<List_items> list_items_arr = new ArrayList<List_items>();
从服务器获取响应并解析它

JSONObject jsono = new JSONObject(json);

JSONArray items = jsono.getJSONArray("items");

  for(int i=0;i<items.length();i++){
         JSONObject list_obj=items.getJSONObject(i);
            List_items list_items = new List_items();
            list_items.setItem_name(list_obj.getString("id"));
            list_items.setItem_sku(list_obj.getString("name"));
            list_items.setItem_id(list_obj.getString("image"));
     list_items_arr .add(list_items);
 }
JSONObject jsono=newjsonobject(json);
JSONArray items=jsono.getJSONArray(“items”);

对于(inti=0;i替换您的
JSONArray mJsonArray=newjsonarray(sb)
code


因为您的json数据第一个标记是JSONObject而不是JSONArray,所以尝试一下它将对您有效

替换您的
JSONArray mJsonArray=new JSONArray(sb)
code


因为您的json数据第一个标记是JSONObject而不是JSONArray,尝试一下它会对您有用

为什么要将它作为json数据保留,需要时从json获取数据?它的服务器响应是json格式。您可以在需要时获取数据。对不起,我的意思是为什么不作为json保留它为什么要作为json数据保留,以及在需要时从json获取数据?它,s server以json格式响应。您可以在需要时获取数据。抱歉,我的意思是为什么不按json的方式保存此代码,但有时会出现try-catch错误,我看不到任何错误。有时会花费很长时间,大约1-2分钟,最后大部分时间都不会失败。为什么会这样?此代码可以完成任务,但有时会失败try-catch出错,我看不到任何错误。有时需要很长的时间,大约1-2分钟,最终大部分是n0t失败。为什么会这样?这段代码很有用,但有时它会转到try-catch错误,我看不到任何错误。有时需要很长时间,大约1-2分钟,大部分是n0t失败