I am trying to parse Json data from this link a link
And I parse all of it but now I want to parse images. When I try to parse it I am parsing all images in the json, but I want just the image of the item.
This is my response code:
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONObject("feed").getJSONArray("entry");
for (int i = 0; i < jsonArray.length(); i++) {
JSONArray imageArray = response.getJSONObject("feed").getJSONArray("entry").getJSONObject(i).getJSONArray("im:image");
for (int j =0; j < imageArray.length(); j++) {
String image = response.getJSONObject("feed").getJSONArray("entry").getJSONObject(i).getJSONArray("im:image").getJSONObject(j).getString("label").toString();
ImagesModule imagesModule = new ImagesModule();
imagesModule.setImageUrl(image);
imagesModules.add(imagesModule);
}}
imageRecyclerViewadapter = new ImageViewAdapter(imagesModules,getContext());
AppRecyclerView.setAdapter(imageRecyclerViewadapter);
} catch (JSONException e) {e.printStackTrace();
I'm not really understand if your one is the question or answer to the problem. But if you want parse it by url, instead ImageModule you can try and getting image by image also:
let img = UIImage()
if let url = NSURL(string: image) {
if let data = NSData(contentsOfURL: url) {
img = UIImage(data: data)!
// array.append(img)
}
}
That for iOS!
Related
Parsing JSON array and object in Android
json:https://api.adjaranet.com/api/v1/movies/
I am trying to parse it with the following Java code in Android but unlimited loading when add
moviejson.getJSONObject("genres").getJSONObject("data").getString("primaryName")
half code is
try {
JSONObject jsonObject = new JSONObject(content);
JSONArray jsonArray = jsonObject.getJSONArray("data");
for(int i =0;i<jsonArray.length(); i++){
JSONObject moviejson = jsonArray.getJSONObject(i);
//if (moviejson.getJSONObject("plot").getJSONObject("data").getString("language").equals("GEO")){
arrayList.add(new MovieItem(
moviejson.getString("id"),
moviejson.getJSONObject("posters").getJSONObject("data").getString("240"),
moviejson.getString("primaryName"),
moviejson.getString("secondaryName"),
moviejson.getString("year"),
moviejson.getJSONObject("plot").getJSONObject("data").getString("description"),
moviejson.getJSONObject("rating").getJSONObject("imdb").getString("score"),
moviejson.getJSONObject("covers").getJSONObject("data").getString("1920"),
moviejson.getJSONObject("genres").getJSONObject("data").getString("primaryName")
));
//}
loading.setVisibility(View.GONE);
}
} catch (JSONException e) {
e.printStackTrace();
}
How solve this problem?
In your json content, data inside genres is an array, not object. That's why when you are trying to parse moviejson.getJSONObject("genres").getJSONObject("data") it throws error.
Try
String genreString = "";
int length = moviejson.getJSONObject("genres").getJSONArray("data").length();
for(int j =0 ; j < length ; j++) {
genreString += moviejson.getJSONObject("genres").getJSONArray("data").getJSONObject(j).getString("primaryName");
}
I have a JSONArray and when I try to parse it, an NPE shows and the logcat shows W/System.err: org.json.JSONException: Value at 0 is null. Please help. I have provided my codes below.
JSON Array
[
{
"student_number":"201411870",
"full_name":"Miranda , Andrew Matthew Matera",
"year":"4",
"course":"BSIT"
}
]
Code Snippet
ArrayList<User> userArrayList = new JsonConverter<User>().toArrayList(response, User.class);
JSONArray jsonArray = new JSONArray(userArrayList);
try {
int regStudentNumber = jsonArray.getJSONObject(0).getInt("student_number");
String regFullName = jsonArray.getJSONObject(1).getString("full_name");
int regYear = jsonArray.getJSONObject(2).getInt("year");
String regCourse = jsonArray.getJSONObject(3).getString("course");
tvStudentNumber.setText(String.valueOf(regStudentNumber));
tvFullName.setText(regFullName);
tvYear.setText(String.valueOf(regYear));
tvCourse.setText(regCourse);
} catch (JSONException e) {
e.printStackTrace();
}
Solved it on my own. Sorry for the unclear question.
try {
JSONArray jsonArray = new JSONArray(response);
if(jsonArray != null) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
int regStudentNumber = jsonObject.getInt("student_number");
String regFullName = jsonObject.getString("full_name");
int regYear = jsonObject.getInt("year");
String regCourse = jsonObject.getString("course");
tvStudentNumber.setText(String.valueOf(regStudentNumber));
tvFullName.setText(regFullName);
tvYear.setText(String.valueOf(regYear));
tvCourse.setText(regCourse);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
First of all parse JSON array to object to make clear it is for a particular value then fetch things from it based on need.
To parse json array to object use code
try {
JSONArray jsonArray = new JSONArray(response);
if(jsonArray != null) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
Below is my JSON. I need to parse the news_title in the "newsdetail" from the category list object. What is the correct way to parse this data? please help me.
{"categorylist":[{"process":"news","ncid":"1"","newsdetail":[{"newsid":"604","category":"1","category_title":"","news_title":"just"},{"newsid":"606","category":"1","category_title":"","news_title":"fg"}]},{"process":"news","ncid":"2","newsdetail":[{"newsid":"477","category":"2","category_title":"","news_title":"way},{"newsid":"478","category":"1","category_title":"","news_title":"k"}]}
{"categorylist":[{"process":"news","ncid":"1"" // cannot be double quotes come here.
,"newsdetail":[{"newsid":"604","category":"1","category_title":"","news_title":"just"},{"newsid":"606","category":"1","category_title":"","news_title":"fg"}]},{"process":"news","ncid":"2","newsdetail":[{"newsid":"477","category":"2","category_title":"","news_title":"way},{"newsid":"478","category":"1","category_title":"","news_title":"k"}]}
you can use below parsor.. but your string data has some issue
try {
JSONObject jsonObject = new JSONObject(data);
JSONArray category_list = jsonObject.getJSONArray("category_list");
for (int i = 0; i < category_list.length() ; i++) {
// here you get news title for each news
String newsTitle = category_list.getJSONObject(i).getString("news_title");
}
}catch (JSONException e){
}
In my program, I am able to fetch all my text. But In my JSOn in a JSON Array there is another JSONArray which have image url. I am getting all my URL in JSON Array when I am using
JSONArray attachments = post.getJSONArray("itemimage");
but when loop is finished, I am getting nothing in my response. How can I replace my ImageView with URLs which is store in attatchment. My Code is here
private void parseResult(String result) {
try {
JSONObject response = new JSONObject(result);
JSONArray posts = response.optJSONArray("items");
GridItem item;
for (int i = 0; i < posts.length(); i++) {
JSONObject post = posts.optJSONObject(i);
String title = post.optString("itemname");
item = new GridItem();
item.setTitle(title);
JSONArray attachments = post.getJSONArray("itemimage");
if (null != attachments && attachments.length() > 0) {
JSONObject attachment = attachments.getJSONObject(0);
if (attachment != null)
item.setImage(attachment.getString(""));
}
mGridData.add(item);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
Um,i am 2 mins late post it out...cuz writing the demo for u.
The parse method you need is below,and you can use framework like Universal Image Loader to load the url to your imageView.
private String parseaaa(String jsonString) {
String imgUrl = "";
try {
JSONObject mJsonObject = new JSONObject(jsonString);
JSONArray mArray = mJsonObject.getJSONArray("items");
for (int i = 0; i < mArray.length(); i++) {
JSONObject object=mArray.getJSONObject(i);
imgUrl+=object.getString("itemimage")+"\n";
Log.i(TAG, imgUrl);
}
} catch (Exception e) {
// TODO: handle exception
}
return imgUrl;
}
BTW,just use JSONArray to load things between [] and JSONObject to load the obj... It's easy to do.
As I can see your JSON you should get JSONException at
JSONObject attachment = attachments.getJSONObject(0);
because your are creating object which is not present there, So I would suggest to simply extract String from your JSONArray and do your setImage stuff..
if (null != attachments && attachments.length() > 0) {
String image= (String) attachments.get(0);
//--- set your image here--//
}
I am trying to get a list of available numbers from the following json object, using the class from org.json
{
"response":true,
"state":1,
"data":
{
"CALLERID":"81101099",
"numbers":
[
"21344111","21772917",
"28511113","29274472",
"29843999","29845591",
"30870001","30870089",
"30870090","30870091"
]
}
}
My first steps were, after receiving the json object from the web service:
jsonObj = new JSONObject(response);
jsonData = jsonObj.optJSONObject("data");
Now, how do I save the string array of numbers?
use:
jsonObj = new JSONObject(response);
jsonData = jsonObj.optJSONObject("data");
JSONArray arrJson = jsonData.getJSONArray("numbers");
String[] arr = new String[arrJson.length()];
for(int i = 0; i < arrJson.length(); i++)
arr[i] = arrJson.getString(i);
you need to use JSONArray to pull data in an array
JSONObject jObj= new JSONObject(your_json_response);
JSONArray array = jObj.getJSONArray("data");
Assuming that you are trying to get it in a javascript block, Try something like this
var arrNumber = jsonData.numbers;
My code is for getting "data":
public void jsonParserArray(String json) {
String [] resultsNumbers = new String[100];
try {
JSONObject jsonObjectGetData = new JSONObject(json);
JSONObject jsonObjectGetNumbers = jsonObjectGetData.optJSONObject("results");
JSONArray jsonArray = jsonObjectGetNumbers.getJSONArray("numbers");
for (int i = 0; i < jsonArray.length(); i++) {
resultsNumbers[i] = jsonArray.getString(i);
}
} catch (JSONException e) {
e.printStackTrace();
Log.e(LOG_TAG, e.toString());
}
}