从api返回多个order_id的值

时间:2017-09-20 06:00:22

标签: java php android

我有一个api函数,如果订单来自Multiple restaurant,我会使用Multiple order id。 这是我得到的回应。

t.sim = Vectorize(function(n, es, n.sim){

  d = numeric(n.sim)
  p = numeric(n.sim)

  for(i in 1:n.sim){
    N = sqrt((n^2)/(2*n))
    x = rnorm(n, es, 1)
    y = rnorm(n, 0, 1)
    a = t.test(x, y, var.equal = TRUE)
    d[i] = a[[1]]/N
    p[i] = a[[3]]
  }
  par(mfcol = c(2, length(n)))
  hist(p) ; hist(d) 
}, "n")
# Example of use:
t.sim(n = c(30, 300), es = .1, n.sim = 1e3) # `n` is a vector of `2` so I expect 
                                            #  4 histograms in my graphical device

这是我的代码,它在Json中返回值

["00000303", "00000304"]

从上面的代码中,我得到了这个结果 $new_order_ids[] = $order_id; if(isset($order_id)) { $order_id = intval($order_id); $order_id++; } else { $order_id = 1; } $order_id = str_pad($order_id, 8, '0', STR_PAD_LEFT); $r_data['order_id'] = $new_order_ids; $json['order_id'] = $order_info['order_id'];

在java中,我使用下面的代码行来获取响应。

["00000303", "00000304"]

这给了我if(response.has("order_id")) order_id = response.getString("order_id");

我如何才能拥有像“00000303”这样的order_id?

2 个答案:

答案 0 :(得分:3)

Java代码

JSONArray orders=response.order_id;
String orderId;
for(int i=0; i<orders.length(); i++){
    orderId = orders.getString(i);
    // do whatever with the orderId
}

PHP代码

function () { 
    $order_ids = array();
    array_push($order_ids, 'order id 1');
    array_push($order_ids, 'order id 2');

    return json_encode({'order_id', $order_id});
}

<强>更新

在解析android

中的JSON响应时,请记住处理JSONException
try {
    JSONArray orders=response.order_id;
    String orderId;
    for(int i=0; i<orders.length(); i++){
        orderId = orders.getString(i);
        // do whatever with the orderId
    }
}
catch(JSONException err) {
    err.printStackTrace();
}

答案 1 :(得分:1)

试试这个

try {

    JSONArray orderArray=new JSONArray(response);
    for (int i = 0; i < orderArray.length(); i++) {
        String orderId=orderArray.getInt(i);
        Log.e("orderId", i+"="+orderId);
    }
 } catch (JSONException e) {
    e.printStackTrace();
}