Reputation: 15
I need to receive the "extract" part of the following Json: https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro&explaintext&redirects=1&titles=turkey
With my current code i receive the whole Json and dont really know how to go on:
public class MainActivity extends AppCompatActivity {
private Button btnSendRequest;
private TextView tvText;
private RequestQueue mRequestQueue;
private StringRequest stringRequest;
private JsonObjectRequest jsonObjectRequest;
private String url = "https://de.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro&explaintext&redirects=1&titles=ilmenau";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSendRequest = (Button) findViewById(R.id.btnSendRequest);
tvText = (TextView) findViewById(R.id.tvText);
btnSendRequest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//send request and print the response using volley library
sendRequestAndPrintResponse();
}
});
}
private void sendRequestAndPrintResponse() {
mRequestQueue = Volley.newRequestQueue(this);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
tvText.setText("Response : " + response.toString());
// Log.i(TAG,"Response : " + response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
tvText.setText("Error : " + error.toString());
//Log.i(TAG,"Error : " + error.toString());
}
});
mRequestQueue.add(jsonObjectRequest);
}
}
So currently I am getting the whole Json back, but I only need to get the "extract" part of the Json. Can anyone help me?
Upvotes: 1
Views: 754
Reputation: 2642
To extract a piece of JSON Object (another JSON Object, Integer, String etc.) you don't have to use any external libraries like GSON. You can manage it with the following code.
JSONObject jObj = new JSONObject(response);
String InterestString = jObj.getJSONObject("local").getString("interests");
Upvotes: 0
Reputation: 4220
Try this
try {
JSONObject jsonObject = new JSONObject(response.toString());
Iterator < String > keys = jsonObject.keys();
while (keys.hasNext()) {
String key = keys.next();
String value = jsonObject.getString(key);
Log.v("key", key);
Log.v("value", value);
if (key.equals("query")) {
JSONObject jsonObject1 = new JSONObject(jsonObject.getString(key));
Iterator < String > keysinner = jsonObject1.keys();
while (keysinner.hasNext()) {
String key1 = keysinner.next();
String value1 = jsonObject1.getString(key1);
Log.e("key1", key1);
Log.e("value1", value1);
if (key1.equals("pages")) {
JSONObject jsonObject2 = new JSONObject(jsonObject1.getString(key1));
Iterator < String > keyPagenumber = jsonObject2.keys();
while (keyPagenumber.hasNext()) {
String keyPageNumber = keyPagenumber.next();
String valuePagenumber = jsonObject2.getString(keyPageNumber);
Log.e("Pages key :", keyPageNumber);
Log.e("Pages value :", valuePagenumber);
if (key1.equals("pages")) {
JSONObject jsonObjectPages = new JSONObject(jsonObject2.getString(keyPageNumber));
Iterator < String > keyPages = jsonObjectPages.keys();
while (keyPages.hasNext()) {
String keyPageDetail = keyPages.next();
String valuePageDetail = jsonObjectPages.getString(keyPageDetail);
Log.e("PagesDetail key :", keyPageDetail);
Log.e("PagesDetail value :", valuePageDetail);
}
}
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
OUTPUT
Upvotes: 1
Reputation: 2950
Something like
JSONObject pages = response.getJSONObject("query").getJSONObject("pages");
JSONObject firstPage = pages.getJSONObject(pages.names().get(0));
String extract = firstPage.getString("extract");
should work.
Upvotes: 0
Reputation: 1
private void sendRequestAndPrintResponse() {
mRequestQueue = Volley.newRequestQueue(this);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject query = response.getJSONObject("query");
JSONObject pages = query.getJSONObject("pages");
JSONObject nos = pages.getJSONObject("11125639");
String extract = nos.getString("extract");
Log.e(TAG, "onResponse: " + extract);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
tvText.setText("Error : " + error.toString());
//Log.i(TAG,"Error : " + error.toString());
}
});
mRequestQueue.add(jsonObjectRequest);
}
modify your code add these lines like this you should get extract
Upvotes: -1
Reputation: 1849
Use GSON library!
So let's say that you deduce the whole json content of your jsonObjectRequest
as a string that i will name theWholeContentOfYourJsonObjectRequestAsAString
JsonParser parser = new JsonParser();
JsonElement jsonTree = parser.parse(theWholeContentOfYourJsonObjectRequestAsAString);
JsonObject jsonObject = jsonTree.getAsJsonObject();
JsonElement f1 = jsonObject.get("extract");
Upvotes: 0