JSONObject数据发送到JSONArray

时间:2018-02-28 03:39:11

标签: java android json

JSONObject data = response.getJSONObject("data");

            Iterator x = data.keys();
            JSONArray jsonArray = new JSONArray();

            while (x.hasNext()){
              String key = (String) x.next();
              jsonArray.put(data.get(key));
            }
            Log.d("TZX", String.valueOf(jsonArray));

我对像这样的可变数据有JSONObject

{"type":"DISKON","result":"DUPE","data":{"1":{"Code":"DISC2","Description":"DISC SALES","Value":0},"2":{"Code":"DISC1.5%","Description":"DISC1.5%","Value":1.5},"3":{"Code":"DISC 2%","Description":"DISC 2%","Value":2}}}

如何在JSONObject上获取对象“值”以便在变量“jsonArray”上发送到JSONArray

抱歉我的语法,我是新手 谢谢

3 个答案:

答案 0 :(得分:2)

试试这个,

<?php

namespace App\Http\Requests;

class MyRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
    */
    public function authorize()
    {
       return true;
     }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
             'first_field' => 'min:0',
             'second_field' => 'min:'.$this->first_field,
        ];
   }
}

答案 1 :(得分:0)

您正在使用数组,因此请直接使用JsonArray来解析数据,如

JSONArray array = response.getJSONArray("data");
List<Integer> values = new ArrayList<>();
   for(int i=0;i < array.length();i++){
        values.add(array.getJSONObject(i).getInt("Value"));
   }

答案 2 :(得分:0)

你变量puts a[0].object_id # 47369457807480 puts a[1].object_id # 47369457807320 puts a[2].object_id # 47369457807200 puts b[0].object_id # 47369457807070 puts b[1].object_id # 47369457807070 puts b[2].object_id # 47369457807070 不是JSONObject它的JSONArray。

data

从数组

获取JSONObject
data = [{"Code":"DISC2","Description":"DISC SALES","Value":0},{"Code":"DISC1.5%","Description":"DISC1.5%","Value":1.5},{"Code":"DISC 2%","Description":"DISC 2%","Value":2}]