本文介绍了JSON文件不工作/不读jsonarray不能转换到的JSONObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我要访问我的Andr​​oid应用我的网页记录。通过这样做,我用JSON做到这一点,PHP。这是我的JSON文件的URL:

但问题是,它只是显示了PHP的code。我需要能够访问/读取该文件。 :(任何想法我这里我做什么?帮助是我pciated多少AP $ P $。感谢。

这是我到目前为止已经试过:

 < PHP    包括('connectdb.php');
    $的SQL =SELECT salesordercard_ code,location_from,location_to,业务员code从salesorderingcard
    $结果= mysql_query($的SQL);
    如果($结果=== FALSE){
     死亡(mysql_error()); // TODO:更好的错误处理
    }
    $集=阵列();
    而($ ROW1 = mysql_fetch_assoc($结果)){
        $设置[] = $ ROW1;
    }    标题(内容类型:应用程序/ JSON');
    回声json_en code($套);
    ?>

MainActivity.class

  @覆盖
保护无效doInBackground(虚空...... PARAMS){
        //创建数组
        ArrayList的=新的ArrayList<&HashMap的LT;字符串,字符串>>();
        // Retrive JSON从JSONfunctions.class指定网站的网址对象
        的JSONObject = JSONFunctions.getJSONfromURL(http://www.shoppersgroup.net/vanmanagement/results.php);        尝试{
            //找到数组名称
            jsonarray = jsonobject.getJSONArray(上岗);            的for(int i = 0; I< jsonarray.length();我++){
                HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();
                的JSONObject = jsonarray.getJSONObject(ⅰ);
                //Log.i(MainActivity.class.getName(),jsonobject.getString(MOVIE_NAME));
                // Retrive JSON对象
                map.put(TAG_ code,jsonobject.getString(salesordercard_ code));
                map.put(TAG_LOCATION_FROM,jsonobject.getString(location_from));
                map.put(TAG_LOCATION_TO,jsonobject.getString(location_to));
                //设置JSON对象到数组
                arraylist.add(地图);
            }
        }赶上(JSONException E){
            Log.e(错误,e.getMessage());
            e.printStackTrace();
        }
        返回null;
}

logcat的:

 二月11日至18日:35:08.521:E / log_tag(1047):错误分析数据org.json.JSONException:值[{\"salesman$c$c\":\"SLMAN001\",\"location_to\":\"MAIN\",\"location_from\":\"IN-TRANSIT\",\"salesordercard_$c$c\":\"SLESO0001\"},{\"salesman$c$c\":\"SLMAN001\",\"location_to\":\"MAIN\",\"location_from\":\"IN-TRANSIT\",\"salesordercard_$c$c\":\"SLESO0002\"}]型的org.json.JSONArray不能转换到的JSONObject


解决方案

首先作为JSON字符串

  [//它的一个阵列
    上岗,//它不是一个数组其指数为0,因此我们将不会使用索引0
    {//索引1
        salesordercard_ code:SLESO0001
        location_from:在途
        location_to:主
        推销员code:SLMAN001
    },
    {//索引2
        salesordercard_ code:SLESO0002
        location_from:在途
        location_to:主
        推销员code:SLMAN001
    }
]

更改doInBackground()方法像下面,

  @覆盖
保护无效doInBackground(虚空...... PARAMS){
        //创建数组
        ArrayList的=新的ArrayList<&HashMap的LT;字符串,字符串>>();
        // Retrive JSON从JSONfunctions.class指定网站的网址对象
        的JSONObject = JSONFunctions.getJSONfromURL(http://www.shoppersgroup.net/vanmanagement/results.php);        尝试{
            //找到数组名称
            jsonarray =新JSONArray(jsonobject.toString());            的for(int i = 1; I< jsonarray.length();我++){//从指数1 STRAT
                HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();
                JSONObject的jsonobj = jsonarray.getJSONObject(I)
                //Log.i(MainActivity.class.getName(),jsonobj.getString(MOVIE_NAME));
                // Retrive JSON对象
                map.put(TAG_ code,jsonobj.getString(salesordercard_ code));
                map.put(TAG_LOCATION_FROM,jsonobj.getString(location_from));
                map.put(TAG_LOCATION_TO,jsonobj.getString(location_to));
                //设置JSON对象到数组
                arraylist.add(地图);
            }
        }赶上(JSONException E){
            Log.e(错误,e.getMessage());
            e.printStackTrace();
        }
        返回null;
}

希望这将解决您的问题。

Hello I want to access my web records in my android app. By doing so, I use JSON to do that and PHP. This is the url of my json file: here

But the problem is it's just displaying the php code. I need to be able to access/read that file. :( Any ideas what I am doing in here? Help is much appreciated by me. thanks.

This is what I've tried so far:

    <?php

    include('connectdb.php');
    $sql = "SELECT salesordercard_code, location_from, location_to, salesmancode FROM salesorderingcard";
    $result = mysql_query($sql);
    if($result === FALSE) {
     die(mysql_error()); // TODO: better error handling
    }
    $set = array();
    while($row1 = mysql_fetch_assoc($result)) {
        $set[] = $row1;
    }

    header('Content-type: application/json');
    echo json_encode($set);


    ?>

MainActivity.class

@Override
protected Void doInBackground(Void... params) {
        // Create the array
        arraylist = new ArrayList<HashMap<String, String>>();
        // Retrive JSON Objects from the given website URL in JSONfunctions.class
        jsonobject = JSONFunctions.getJSONfromURL("http://www.shoppersgroup.net/vanmanagement/results.php");

        try {
            // Locate the array name
            jsonarray = jsonobject.getJSONArray("posts");

            for (int i = 0; i < jsonarray.length(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                jsonobject = jsonarray.getJSONObject(i);
                //Log.i(MainActivity.class.getName(), jsonobject.getString("movie_name"));
                // Retrive JSON Objects
                map.put(TAG_CODE, jsonobject.getString("salesordercard_code"));
                map.put(TAG_LOCATION_FROM, jsonobject.getString("location_from"));
                map.put(TAG_LOCATION_TO, jsonobject.getString("location_to"));
                // Set the JSON Objects into the array
                arraylist.add(map);
            }
        } catch (JSONException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
}

Logcat:

            11-18 02:35:08.521: E/log_tag(1047): Error parsing data org.json.JSONException: Value [{"salesmancode":"SLMAN001","location_to":"MAIN","location_from":"IN-TRANSIT","salesordercard_code":"SLESO0001"},{"salesmancode":"SLMAN001","location_to":"MAIN","location_from":"IN-TRANSIT","salesordercard_code":"SLESO0002"}] of type org.json.JSONArray cannot be converted to JSONObject
解决方案

First as Json String

[ // Its an Array
    "posts",// Its not an Array and its index is 0 so we will not use index 0
    {//Index1
        "salesordercard_code": "SLESO0001",
        "location_from": "IN-TRANSIT",
        "location_to": "MAIN",
        "salesmancode": "SLMAN001"
    },
    {//index2
        "salesordercard_code": "SLESO0002",
        "location_from": "IN-TRANSIT",
        "location_to": "MAIN",
        "salesmancode": "SLMAN001"
    }
]

Change your doInBackground() method like below,

@Override
protected Void doInBackground(Void... params) {
        // Create the array
        arraylist = new ArrayList<HashMap<String, String>>();
        // Retrive JSON Objects from the given website URL in JSONfunctions.class
        jsonobject = JSONFunctions.getJSONfromURL("http://www.shoppersgroup.net/vanmanagement/results.php");

        try {
            // Locate the array name
            jsonarray = new JSONArray(jsonobject.toString());

            for (int i = 1; i < jsonarray.length(); i++) {// strat from index1
                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject jsonobj = jsonarray.getJSONObject(i);
                //Log.i(MainActivity.class.getName(), jsonobj.getString("movie_name"));
                // Retrive JSON Objects
                map.put(TAG_CODE, jsonobj.getString("salesordercard_code"));
                map.put(TAG_LOCATION_FROM, jsonobj.getString("location_from"));
                map.put(TAG_LOCATION_TO, jsonobj.getString("location_to"));
                // Set the JSON Objects into the array
                arraylist.add(map);
            }
        } catch (JSONException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
}

Hope this will solve your problem.

这篇关于JSON文件不工作/不读jsonarray不能转换到的JSONObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 21:20