如何将迭代器中的值存储到局部变量?

时间:2021-01-05 14:01:13

My Output is :

我的输出是:

I/System.out: Iterator values :{"name":"SMhack","permittedUsers":[1,2,3,4,5,6],"id":2,"profilesAttached":[1,2,3,4,5,6,8,12,13,14,15,16,17,18,19,20,21,24,25,26,27,28,29,30,31]}

i want to take the value of "id" and store it in a local variable. how can i store the value.

我想取“id”的值并将其存储在局部变量中。我该如何存储价值。

My Code is :

我的代码是:

Iterator<JSONObject> iterator = org.iterator();
            while (iterator.hasNext())
            {
                System.out.println("Iterator values :"+iterator.next());

                //here i want to store the value from iterator to be stored to a local variable named (org_id).    

            }

2 个解决方案

#1


1  

You can use JSONObject.getInt() method:

您可以使用JSONObject.getInt()方法:

Iterator<JSONObject> iterator = org.iterator();
while (iterator.hasNext()) {
    JSONObject current = iterator.next();   
    System.out.println("Iterator values : " + current);

    int orgId = current.getInt("id");

}

#2


0  

You are using org.json.simple.JSONObject which implements java.util.Map. So I think you will need to do something like this:

您正在使用实现java.util.Map的org.json.simple.JSONObject。所以我认为你需要做这样的事情:

Iterator<JSONObject> iterator = org.iterator();
while (iterator.hasNext())
{
    JSONObject obj = iterator.next();
    int orgId = (int) obj.get("id");
    // Do something with orgId
}

#1


1  

You can use JSONObject.getInt() method:

您可以使用JSONObject.getInt()方法:

Iterator<JSONObject> iterator = org.iterator();
while (iterator.hasNext()) {
    JSONObject current = iterator.next();   
    System.out.println("Iterator values : " + current);

    int orgId = current.getInt("id");

}

#2


0  

You are using org.json.simple.JSONObject which implements java.util.Map. So I think you will need to do something like this:

您正在使用实现java.util.Map的org.json.simple.JSONObject。所以我认为你需要做这样的事情:

Iterator<JSONObject> iterator = org.iterator();
while (iterator.hasNext())
{
    JSONObject obj = iterator.next();
    int orgId = (int) obj.get("id");
    // Do something with orgId
}