all the names are not fetching from arraylist - json

Json file show on arraylist, I want to show all the names from the arraylist from the json file.
try {
JSONArray root = new JSONArray(json);
for (int i = 0; i < root.length(); i++)
{
JSONObject att = (JSONObject) root.getJSONObject(i);
name = att.getString("nm");
list.add(name);
}
for(int i = 0; i<root.length(); i++){
Log.i("name", name);
}
}catch (JSONException e) {
e.printStackTrace();
}

try{
JSONArray root = new JSONArray(json);
for (int i = 0; i < root.length(); i++){
JSONObject att = (JSONObject) root.getJSONObject(i);
name = att.getString("nm");
list.add(name);
}
for(int j = 0; j<feedList.size(); j++){
Log.i("name",feedList.get(j));
}
}catch (JSONException e) {
e.printStackTrace();
}

Related

Here i want to pass each json object to another activity and set that object value to some textview

pass the vehiclename object to another activity
and set that value as a text view
Please Help Me
JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
public void onResponse(JSONObject response) {
try {
JSONObject object1 = response.getJSONObject("liveMapResponseVO");
JSONArray jsonArray = object1.getJSONArray("locations");
for(int i=0; i<jsonArray.length(); i++){
JSONObject jsonObject = (JSONObject) jsonArray.get(i);
Movie movie = new Movie();
movie.setTitle(jsonObject.getString("vehicleName"));
movie.setRating(jsonObject.getString("speed"));
movie.setGenre(jsonObject.getString("stopTime"));
movie.setYear(jsonObject.getString("idleTime"));
Log.d(TAG, jsonObject.getString("vehicleName"));
movieList.add(movie);
}
pDialog.hide();
} catch (JSONException e) {
e.printStackTrace();
}
adapter.notifyDataSetChanged();
}

JSON object in for loop keeps only last record

I have this code
JSONObject output = new JSONObject();
JSONObject elements = new JSONObject();
JSONArray jsonArrayOutput = new JSONArray();
ArrayList<String> name = ArrayList<String>();
for (int i=0 ; i<name.size() ; i++){
elements.put("Name", name.get(i));
jsonArrrayOutput.put(elements);
}
output.put("Results", jsonArrrayOutput).toString();
The problem is that the resulted output Json has only the last element of "name" arraylist many times, not all elements.
How can I fix it?
You are adding elements again same name key in jsonArrayOutput
try creating a new JSONObject for each iteration.
E.g.:
JSONObject output = new JSONObject();
JSONObject elements = new JSONObject();
JSONArray jsonArrayOutput = new JSONArray();
ArrayList<String> name = ArrayList<String>();
for (int i=0 ; i<name.size() ; i++){
JSONObject temp = new JSONObject();
temp.put("Name", name.get(i));
jsonArrrayOutput.put(temp);
}
output.put("Results", jsonArrrayOutput).toString();
Here is my version of your code. The problem with your code is the declaration of your elements object. Whenever you changed the elements that would change the element and the element that you added to the array.
This happens because reference is used when you put the element object into the jsonArrayOutput
JSONObject output = new JSONObject();
JSONArray jsonArrayOutput = new JSONArray();
ArrayList<String> name = new ArrayList<>();
for (int i = 0; i < name.size(); i++) {
JSONObject elements = new JSONObject();
try {
elements.put("Name", name.get(i));
jsonArrayOutput.put(elements);
} catch (JSONException e) {
e.printStackTrace();
}
}
try {
output.put("Results", jsonArrayOutput).toString();
Log.i("info",output.toString());
} catch (JSONException e) {
e.printStackTrace();
}
Hope that helps!

ResponseException in jaunt

Here is the error
message: UserAgent.sendGET; response error
requestUrl: https://www.linkedin.com/directory/topics-c/
response:
requestURL: https://www.linkedin.com/directory/topics-c/
status: 999
here is my code
try {
Document doc = userAgent.visit(link);
Elements eles = doc.findEvery("<ul class=\"column quad-column\">");
for (int i = 0; i < eles.size(); i++) {
Elements href_keywords = eles.getElement(i).findEvery("<a href>");
for (int j = 0; j < href_keywords.size(); j++) {
keywords.add(href_keywords.getElement(j).getText());
}
}
you should find the elements like this:
Elements eles = userAgent.doc.findEvery("");
here is the full code:
package scrap;
import com.jaunt.*;
public class Scrap {
public static void main(String[] args) {
try {
UserAgent userAgent = new UserAgent();
userAgent.visit("https://www.linkedin.com/directory/topics-c/");
// System.out.println(userAgent.doc.innerHTML());
Elements eles = userAgent.doc.findEvery("<ul class=\"column quad-column\">");
for (int i = 0; i < eles.size(); i++) {
Elements href_keywords = eles.getElement(i).findEvery("<a href>");
for (int j = 0; j < href_keywords.size(); j++) {
/// here add to your LIST
System.out.println(href_keywords.getElement(j).getText());
}
}
} catch (JauntException e) {
System.err.println(e);
}
}
}

Export all ElasticSearch data to JSON file

I need t export all data in the ElasticSearch and reindex all those data.
The export Java code as follows.
SearchResponse response = client.prepareSearch("news")
.setTypes("news_data")
.setQuery(QueryBuilders.matchAllQuery())
.setSize(1000)
.setScroll(new TimeValue(600000))
.setSearchType(SearchType.SCAN)
.execute().actionGet();
String scrollid = response.getScrollId();
try {
//把导出的结果以JSON的格式写到文件里
BufferedWriter out = new BufferedWriter(new FileWriter("es", true));
while (true) {
SearchResponse response2 = client.prepareSearchScroll(scrollid)
.setScroll(new TimeValue(1000000))
.execute().actionGet();
SearchHits searchHit = response2.getHits();
//再次查询不到数据时跳出循环
if (searchHit.getHits().length == 0) {
break;
}
System.out.println("查询数量 :" + searchHit.getHits().length);
for (int i = 0; i < searchHit.getHits().length; i++) {
String json = searchHit.getHits()[i].getSourceAsString();
out.write(json);
out.write("\r\n");
}
}
System.out.println("查询结束");
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The numbers of documents is about 140W. Use this java code 60W documents can be exported and throw an exception.
远程主机强迫关闭了一个现有的连接
You have to use the scrollid from the previous response for your next request.
See https://www.elastic.co/guide/en/elasticsearch/reference/1.7/search-request-scroll.html#scroll-scan for more details
Perhaps you can try something like this instead?
SearchResponse response = client.prepareSearch("news")
.setTypes("news_data")
.setQuery(QueryBuilders.matchAllQuery())
.setSize(1000)
.setScroll(new TimeValue(600000))
.setSearchType(SearchType.SCAN)
.execute().actionGet();
int sequence = 0;
do
{
response = client.prepareSearchScroll(response.getScrollId())
.setScroll(new TimeValue(600000))
.execute().actionGet();
if (response.getHits().getHits().length > 0)
{
try
{
final BufferedWriter out = new BufferedWriter(new FileWriter("es-" + (++sequence) , true));
for (final SearchHit hit : response.getHits().getHits())
{
out.write(hit.getSourceAsString());
out.write("\r\n");
}
out.close();
}
catch (final IOException e)
{
e.printStackTrace();
}
}
}
while (response.getHits().hits().length > 0);

Getting JSON Exception no value for?

I am trying to handle the json response, that is like
{"Status":true,"UserId":111,"FirstName":"dev","LastName":"dev","Gender":-1,"BirthdayDate":"0000-00-00","Phone":"","ProfilePicture":"","ProfilePicture60px":"","ProfilePicture120px":"","CountryId":-1,"Email":"droidwithmxxmail.com","Password":"******123","RegisterDate":"2015-05-08 20:08:07","SessionId":"fce248fe6499b7a9338a1b64554509eb77841"}
but getting org.json.JSONException: no value for exception
My code is this.
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
allres =jsonObj.getJSONArray(jsonStr);
for (int i = 0; i < allres.length(); i++) {
JSONObject c = allres.getJSONObject(i);
userId = c.getString("UserId");
}
} catch (JSONException e) {
e.printStackTrace();
}
Try this code:
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
userId = jsonObj.getString("UserId");
} catch (JSONException e) {
e.printStackTrace();
}
Your JSON string doesn't have an array in it, so the object that you will be getting from the first JSONObject is already where your information lives.