从php sql server对数组进行json编码并转换为Android

从php sql server对数组进行json编码并转换为Android,php,android,sql-server,json,Php,Android,Sql Server,Json,嗨,我是android php客户端服务器的新手。目前,我正在从php sql server向Android客户端发送来自sql行的多个结果的响应。之前,我发送了一个简单的字符串并收到如下android: $result_data = array( 'ResultArray' => 'success', ); #Output the JSON data echo json_encode($result_data); 然后在android中: // Create a JSON

嗨,我是android php客户端服务器的新手。目前,我正在从php sql server向Android客户端发送来自sql行的多个结果的响应。之前,我发送了一个简单的字符串并收到如下android:

$result_data = array( 
'ResultArray' => 'success',
); 

 #Output the JSON data 
 echo json_encode($result_data); 
然后在android中:

// Create a JSON object from the request response
JSONObject jsonObject = new JSONObject(result);

//Retrieve the data from the JSON object
String resultLoging = jsonObject.getString("ResultArray");

现在我想从数据库中接收3列:id、phone、name。我该怎么做?感谢您的帮助

使用JSONArray获得多个json结果:

JSONArray jsonArray =  jsonObject.getJSONArray("ResultArray");
迭代JSONArray并从JSONObject获取值:

for (int i=0;i<jsonArray.length();i++){
     JSONObject json = jsonArray.getJSONObject(i);
     String id = json.getString("id");
     String phone = json.getString("phone");
     String name = json.getString("name");
} 
试试这个

$result_data = array( 
'ResultArray' => 'success',
); 

echo json_encode(array('result'=>$result_data));
在android中

     JSONParser jParser = new JSONParser();
     JSONObject json = jParser.getJSONFromUrl("url of php file");
     JsonArray arry = json.getJSONArray("result");
     JSONObject c = arry .getJSONObject(0);
     String resultarr= c.getString("ResultArray");





 public class JSONParser {

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

// 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");
            url += "?" + paramString;
            HttpGet httpGet = new HttpGet(url);

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

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
      //  System.out.println(json);

    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {





        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

   }
  }

刚发现这个,对我的问题来说是最好的


在php中使用以下格式

$result = mysql_query("SELECT *FROM tablename") or die(mysql_error());

// check for empty result

if (mysql_num_rows($result) > 0) 

{

    // looping through all results

    // products node

    $response["details"] = array();

    while ($row = mysql_fetch_array($result))

 {

        // temp user array

        $product = array();

        $product["id"] = $row["id"];

        $product["name"] = $row["name"];

array_push($response["details"], $product);

    }

    // success

    $response["success"] = 1;

    // echoing JSON response

    echo json_encode($response);
在android中

     JSONParser jParser = new JSONParser();
     JSONObject json = jParser.getJSONFromUrl("url of php file");
     JsonArray arry = json.getJSONArray("result");
     JSONObject c = arry .getJSONObject(0);
     String resultarr= c.getString("ResultArray");





 public class JSONParser {

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

// 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");
            url += "?" + paramString;
            HttpGet httpGet = new HttpGet(url);

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

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
      //  System.out.println(json);

    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {





        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

   }
  }
获得成功价值

int success = json.getInt(TAG_SUCCESS);
使用以下格式获取数据

JSONArray spi = json.getJSONArray("details"); 

Use a for loop to get the object values in the array

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

                        JSONObject c = spi.getJSONObject(i);

 id = c.getString("id");

}

你会粘贴你的回答吗?然后我们将以类似的方式为你提供准确的解决方案,就像在这里无数类似的问题中所回答的一样。上面的代码只是我简单的字符串编码示例。我想要具有多个结果的数组,jsonparser来自哪个库?如果它是一个解析器,为什么它有一个名为getJSONFromUrl?id=c.getStringid的方法;这个id是数组还是字符串?