Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
随机访问数组中的值 import org.json.JSONObject; 导入org.json.JSONArray; 导入java.util.Random; int最小值=0; 字符串[]代码_数组=新字符串[10]; 字符串响应=ctx.getPreviousResult().getResponseDataAsString(); 日志信息(响应); JSONObject JSONObject=新JSONObject(响应); JSONArray JSONArray=jsonObject.getJSONArray(“选项”); 整数opt_count=jsonArray.length(); 变量put('count',Integer.toString(opt_count)); 如果(选项计数>1) { 对于(int a=0;a1) { 随机r=新随机(); int随机值=r.nextInt(值计数-最小值)+最小值; 变量put('随机值1',随机值); 整数值= 新建数组.getJSONObject(随机值).getString(“值”); 变量put('FValue',Integer.toString(FValue)); } } } 否则{ 变量put('Attribute','First Attribute'); }_Java_Arrays_Json - Fatal编程技术网

随机访问数组中的值 import org.json.JSONObject; 导入org.json.JSONArray; 导入java.util.Random; int最小值=0; 字符串[]代码_数组=新字符串[10]; 字符串响应=ctx.getPreviousResult().getResponseDataAsString(); 日志信息(响应); JSONObject JSONObject=新JSONObject(响应); JSONArray JSONArray=jsonObject.getJSONArray(“选项”); 整数opt_count=jsonArray.length(); 变量put('count',Integer.toString(opt_count)); 如果(选项计数>1) { 对于(int a=0;a1) { 随机r=新随机(); int随机值=r.nextInt(值计数-最小值)+最小值; 变量put('随机值1',随机值); 整数值= 新建数组.getJSONObject(随机值).getString(“值”); 变量put('FValue',Integer.toString(FValue)); } } } 否则{ 变量put('Attribute','First Attribute'); }

随机访问数组中的值 import org.json.JSONObject; 导入org.json.JSONArray; 导入java.util.Random; int最小值=0; 字符串[]代码_数组=新字符串[10]; 字符串响应=ctx.getPreviousResult().getResponseDataAsString(); 日志信息(响应); JSONObject JSONObject=新JSONObject(响应); JSONArray JSONArray=jsonObject.getJSONArray(“选项”); 整数opt_count=jsonArray.length(); 变量put('count',Integer.toString(opt_count)); 如果(选项计数>1) { 对于(int a=0;a1) { 随机r=新随机(); int随机值=r.nextInt(值计数-最小值)+最小值; 变量put('随机值1',随机值); 整数值= 新建数组.getJSONObject(随机值).getString(“值”); 变量put('FValue',Integer.toString(FValue)); } } } 否则{ 变量put('Attribute','First Attribute'); },java,arrays,json,Java,Arrays,Json,兹附上: 我需要实现相同的:选项[0]。值[0]。值 以上参数[0]的on值是硬编码的。我想随机化并获得值。如果我正确地理解了您的问题,那么您可以尝试类似的方法 import org.json.JSONObject; import org.json.JSONArray; import java.util.Random; int minimum=0; String[] code_array = new String[10]; String response= ctx.getPreviousRe

兹附上:

我需要实现相同的:选项[0]。值[0]。值


以上参数[0]的on值是硬编码的。我想随机化并获得值。

如果我正确地理解了您的问题,那么您可以尝试类似的方法

import org.json.JSONObject;
import org.json.JSONArray;
import java.util.Random;


int minimum=0;
String[] code_array = new String[10];
String response= ctx.getPreviousResult().getResponseDataAsString();
log.info(response);
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("options");
Integer opt_count= jsonArray.length();
vars.put('count',Integer.toString(opt_count));
if(opt_count > 1)
{
for (int a= 0; a < opt_count; a++)
{
    code_array[a] .add(jsonArray.getJSONObject(a).getString("code"));
    log.info(code_array[a]);
    JSONArray  new_array= jsonArray.getJSONObject(a).getJSONArray("values");
    log.info(new_array);
    int Value_count =  new_array.size();
    vars.put('VALUE_COUNT',Integer.toString(Value_count));
    if(Value_count > 1)
    {
        Random r= new Random();
        int random_value = r.nextInt(Value_count - minimum) + minimum;
        vars.put('random_value1',random_value);
        Integer FValue= 
  new_array.getJSONObject(random_value).getString("value");
        vars.put('FValue',Integer.toString(FValue));
    }
   }

}
else{

      vars.put('Attribute','First Attribute');

 }

你到底有什么问题?提出一个随机索引?检索值?@Stultuske两者都有。在哪方面有帮助吗?你能尝试非随机读取数组吗?我们将在这一部分添加随机性。@AxelH,我需要用Java实现这一点。和有什么问题?你试过什么?你在问什么?你在期待什么?如果你真的认为我们在这里做你的工作,你没有阅读常见问题解答。。。你的问题与SO和他们的范围相匹配,我将在那里提供帮助。现在,没有你的努力,只有一个广泛的需求。Naveen,我需要用Java做这件事,这只是Java!Naveen,我需要在Java@Siddish中模拟它,Naveen向您展示的代码是Java。要么他的答案是正确的,要么你的问题是错误的。
public static int getRandomIndex(int min, int max) {

    Random r = new Random();
    return r.nextInt((max - min) + 1) + min;

}


JSONObject jsonObject1 = Array.getJSONObject(getRandomIndex(0,length_of_JSONArray));