J
J
Jsman2016-01-19 12:03:38
Java
Jsman, 2016-01-19 12:03:38

How to get specific parameter in JSON?

Good afternoon! There is a source code that receives a JSON value (contact API, we get the regions of the Russian Federation). An example of what is output to the console:

title
Чувашская
region_id
1160844
title
Чукотский АО
region_id
1160930
title
Ямало-Ненецкий  АО
region_id
1115658
title
Ярославская область

What needs to be changed in the code to get only the values ​​of the regions?
source
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.URL;
import java.net.URLConnection;

public class Main {
    public static void main(String[] args) throws Exception{
        getPage("http://api.vk.com/method/database.getRegions?&country_id=1");
    }

    static String getPage(String page_url) throws IOException {
        URL url = new URL(page_url);
        URLConnection urlConnection = url.openConnection();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

        StringBuilder lines = new StringBuilder();
        String line;
        if ( (line=bufferedReader.readLine()) != null){
            lines.append(line);
        }
        JsonReader reader = new JsonReader(new StringReader(line));
        handleObject(reader);
        return lines.toString();

    }

    private static void handleObject(JsonReader reader) throws IOException
    {
        reader.beginObject();
        while (reader.hasNext()) {
            JsonToken token = reader.peek(); 
            if (token.equals(JsonToken.BEGIN_ARRAY)) { 
                handleArray(reader);
            }
            else if (token.equals(JsonToken.END_OBJECT)) {
                reader.endObject();
                return;
            } else
                handleNonArrayToken(reader, token);
        }

    }

    public static void handleArray(JsonReader reader) throws IOException
    {
        reader.beginArray();
        while (true) {
            JsonToken token = reader.peek();
            if (token.equals(JsonToken.END_ARRAY)) {
                reader.endArray();
                break;
            } else if (token.equals(JsonToken.BEGIN_OBJECT)) {
                handleObject(reader);
            } else if (token.equals(JsonToken.END_OBJECT)) {
                reader.endObject();
            } else
                handleNonArrayToken(reader, token);
        }
    }

    public static void handleNonArrayToken(JsonReader reader, JsonToken token) throws IOException
    {
        if (token.equals(JsonToken.NAME))
            System.out.println(reader.nextName());
        else if (token.equals(JsonToken.STRING))
            System.out.println(reader.nextString());
        else if (token.equals(JsonToken.NUMBER))
            System.out.println(reader.nextDouble());
        else
            reader.skipValue();
    }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Jsman, 2016-01-19
@SeniorDmitry

Alas, but for some reason it didn’t work, maybe I twisted something (
9221574ebe544fab985a904faf7d2320.jpg

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question