如何将json数据解码为php Array

时间:2014-02-23 09:43:39

标签: php android mysql json

我正在尝试将json对象(其中包含pramas中的所有联系人详细信息)转换为php数组,以便它可以在单个go中将这些详细信息存储在Mysql数据库中,但是目前我只收到一个最后一个联系人详细信息数据库,请建议我如何在php中进行循环,以便将所有联系人详细信息保存在数据库中,谢谢,

附上我的Php代码:

// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['name']) && isset($_POST['id']) && isset($_POST['phone'])&& isset($_POST['email'])) {

    $id = $_POST['id'];
    $name = $_POST['name'];
    $phone = $_POST['phone'];
    $email = $_POST['email'];

    // include db connect class
    require_once __DIR__ . '/db_connect.php';

    // connecting to db
    $db = new DB_CONNECT();

    // mysql inserting a new row
    $result = mysql_query("INSERT INTO crm(id, name, phone, email) VALUES('$id', '$name', '$phone', '$email')");

    // check if row inserted or not
    if ($result) {
        // successfully inserted into database
        $response["success"] = 1;
        $response["message"] = "Product successfully created.";

        // echoing JSON response
        echo json_encode($response);
    } else {
        // failed to insert row
        $response["success"] = 0;
        $response["message"] = "Oops! An error occurred.";

        // echoing JSON response
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}
?>

Json解析器类:

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";
    StringBuilder sb;

    // constructor
    public JSONParser() {

    }

    // function get json from url
    // by making HTTP POST or GET mehtod
    public JSONObject makeHttpRequest(String url, String method,
            List<NameValuePair> params) {

        // Making HTTP request
        try {

            // check for request method
            if(method == "POST"){
                // request method is POST
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

            }else if(method == "GET"){
                // request method is GET
                DefaultHttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params, "utf-8");
                Log.d("Hope","Hope 1");
                url += "?" + paramString;
                Log.d("Hope","Hope 2");
                HttpGet httpGet = new HttpGet(url);
                Log.d("Hope","Hope 3");

                HttpResponse httpResponse = httpClient.execute(httpGet);
                Log.d("Hope","Hope 4");
                HttpEntity httpEntity = httpResponse.getEntity();
                Log.d("Hope","Hope 5");
                is = httpEntity.getContent();
            }           

        } catch (UnsupportedEncodingException e) {
            Log.d("ex1","ex1 "+e);
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            Log.d("ex1","ex2 "+e);
            e.printStackTrace();
        } catch (IOException e) {
            Log.d("ex1","ex3 "+e);
            e.printStackTrace();
        }

        try {
            Log.d("Hope","Hope 6");
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            Log.d("Hope","Hope 7");
            sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            Log.d("Hope","Hope 8");
            is.close();
            json = sb.toString();
            Log.d("eee","json"+ json);
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + json);
        }

        //char[] chars = json.toCharArray();
        // try parse the string to a JSON object
        //if(chars[0] != 'D'){
            try {
                jObj = new JSONObject(json);
            } catch (Exception e) {
                Log.e("JSON Parser", "Error parsing data " + e.toString());
            }
        //}
        //else {
        //      Log.d("null","null");
        //  }

    return jObj;


    }
}

0 个答案:

没有答案