0

I have a JSON file. I need to get only array of id values (30461, ...)

 {
    "response":[
    5205,
    {
    "id":30461, // target value!
    "from_id":-52078741,
    "to_id":-52078741,
    "date":1459435510
...

How to receive an array of id values?

JSONParser parser = new JSONParser();
Object obj  = parser.parse(new FileReader("C://json_file.txt"));
JSONArray array = new JSONArray();
JSONObject obj_j = (JSONObject) obj;
array.add(obj_j);
System.out.println(array);

Link to whole json file: https://api.vk.com/method/wall.get?domain=just_eng&offset=1&counts=100

Cat
  • 11
  • 2
  • 3
    Sorry but I am not sure what problem are you facing. Your Json represents object which in `response` key stores array which at `position 1` stores object which has key `id` and you want to get that value. It is quite straightforward parsing. Did I misunderstood you or do you have any specific problem with your code? – Pshemo Mar 31 '16 at 16:53
  • Please share some more piece of json to understand it's structure correctly. – Braj Mar 31 '16 at 16:57
  • @CacheStaheli Based on tags it is json-simple library. – Pshemo Mar 31 '16 at 16:57
  • @Pshemo Totally missed that tag. – Cache Staheli Mar 31 '16 at 16:58
  • Since you are new on Stack Overflow you may not know yet that to update your post you can use [edit] option (placed below your post). You can also post comments under your post (but if you have important information it is better to put it in your question directly rather than making people willing to help you search for it in comments). – Pshemo Mar 31 '16 at 17:01
  • I face with problem: I don't understand this structure. ({"response":[5205,{) - I didn't find same samples. Help me please. – Cat Apr 01 '16 at 06:17
  • I introduce whole json file, which I need to parse. I want to create array of "id" values. Link to json file: https://api.vk.com/method/wall.get?domain=just_eng&offset=1&counts=100 – Cat Apr 01 '16 at 06:20

1 Answers1

0

JSONObject has a method called getJSONArray(Sring name).

so in your case the following should work:

JSONObject obj_j = (JSONObject) obj;
JSONArray array = obj_j.getJSONArray("id");

is that what you want ?

Dimitrios Begnis
  • 823
  • 6
  • 16
  • It is not what I want, because I using json-simple library, which have not a getJSONArray method. – Cat Apr 01 '16 at 05:30
  • oh I see, my mistake... then try this: `JSONArray array= (JSONArray) jsonObject.get("id");` source: [json-simple-example](http://www.mkyong.com/java/json-simple-example-read-and-write-json/) – Dimitrios Begnis Apr 01 '16 at 06:54