how can I fetch this response from URL in android?
1. //main array
[
//array in main array
[
//object in inner array
{
//data to be fetched here
"user_id": "8035",
"sr_no": "MG2459",
"user_type": "2",
"name": "Allen"
}
],
//2nd array in main array
[
{
"user_id": "8035",
"sr_no": "MG2459",
"user_type": "2",
"name": "TestName"
}
]
]
try below code :-
try {
JSONArray ja = new JSONArray(ur string);
for (int i = 0; i < ja.length(); i++)
{
JSONArray ja1 = ja.getJSONArray(i);
for (int j = 0; j < ja1.length(); j++) {
JSONObject jo = ja1.getJSONObject(j);
String user_id = jo.getString("user_id");
String sr_no = jo.getString("sr_no");
String user_type = jo.getString("user_type");
String name = jo.getString("name");
}
}
} catch (Exception e) {
// TODO: handle exception
}
Related
I'm trying to parse a JSON object. The JSON response is as follows.
{
"message": "Success",
"list": [
{
"orderId": 24,
"phoneNumber": "1234567893",
"totalAmount": 100,
"addressBean": {
"cadId": 1,
"phone2": "1234567899",
"address1": "34, gandhi nagar",
}
},
Android code which i have tried for getting "orderId", "phoneNumber" and "totalAmount" is below.
final List<OrderModel> orderList = new ArrayList<>();
public void onResponse(JSONObject response) {
try {
if (response.getString("message").equalsIgnoreCase("Success")) {
JSONArray jArray = response.getJSONArray("list");
for (int i = 0; i < jArray.length(); i++) {
OrderModel model = new OrderModel();
orderModel.setOrderId(jArray.getJSONObject(i).getString("orderId"));
orderModel.setphoneNumber(jArray.getJSONObject(i).getString("phoneNumber"));
orderModel.setTotalAmount(new BigDecimal(jArray.getJSONObject(i).getString("totalAmount")));
orderList.add(orderModel);
}
setOrderList(orderList);
I want to show the "cadId", "phone2" and "address1" in a Textview. How can i do that?
Try to use this Code
try {
JSONArray jArray = response.getJSONArray("list");
for (int i = 0; i < jArray.length(); i++) {
JSONObject jsonObject = jArray.getJSONObject(i);
if (jsonObject.has("addressBean")){
JSONObject addressObject = jsonObject.getJSONObject("addressBean");
int cadId = addressObject.getInt("cadId");
String phone = addressObject.getString("phone2");
String address = addressObject.getString("address1");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
Code for phone2
String phone2 = jArray.getJSONObject(i).getJSONObject("addressBean").getString("phone2");
Use getInt() for cadId
i have get the first array of json but the second array come inside the first array in array ....:
how is solved it
i have get the tweet_image array inside the image array two array how is solve it
i have get the image path;
{
"feed": [
{
"tweet_id": "794",
"userid": "6",
"content": "<a href=http://www.punjabkesari.in/news/article-370994>http://www.punjabkesari.in/news/article-370994</a>",
"favorite_count": "0",
"reply_count": "0",
"retweet_count": "0",
"tweet_location": "",
"created_date": "2015-06-16 11:49:00",
"name": "amar bhanu",
"user_image": "http://sabakuch.com/public/images_upload/avatars/ozone/6_30_imageamar.jpg",
"tweet_images": {
"image": [
"http://sabakuch.com/public/images_upload/tweet/794_400_1434435540_album143443554069.jpg"
]
}
}
]
}
You can try this.
try {
JSONObject _jObject = new JSONObject("YOUR_JSON_STRING");
JSONArray _jArrayFeed = _jObject.getJSONArray("feed");
if (_jArrayFeed.length()>0) {
for (int i = 0; i < _jArrayFeed.length(); i++) {
JSONObject _subObj = _jArrayFeed.getJSONObject(i);
String _tweet_id = _subObj.getString("tweet_id");
String _userid = _subObj.getString("userid");
String _content = _subObj.getString("content");
String _favorite_count = _subObj.getString("favorite_count");
String _reply_count = _subObj.getString("reply_count");
String _retweet_count= _subObj.getString("retweet_count");
String _tweet_location = _subObj.getString("tweet_location");
String _created_date = _subObj.getString("created_date");
String _name = _subObj.getString("name");
String _user_image = _subObj.getString("user_image");
JSONObject _jObjtweet_images = _subObj.getJSONObject("tweet_images");
JSONArray _jArrayImages = _jObjtweet_images.getJSONArray("image");
if (_jArrayImages.length()>0) {
for (int j = 0; j < _jArrayImages.length(); j++) {
String _image = _jArrayImages.getString(j);
}
}
}
}
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
And let me know, if you have any issues.
Try this way
JSONArray array= jsonResponse.getJSONArray("feed");
JSONObject obj= array.getJSONObject(0);
JSONObject image= obj.getJSONObject("tweet_images");
JSONArray image_array= image.getJSONArray("image");
String url= image_array.getString(0);
hope it helps :-)
I want parse all "name" and "desc" from this json object inside another json object. i need another "for"?
{
"main": {
"details": [
{
"owner_name": "owner_name1",
"id": "id1",
"details2": {
"data": [
{
"name": "name1",
"desc": "my desc1",
},
{
"name": "name2",
"desc": "my desc2",
}
],
}
},
{
"owner_name": "owner_name2",
"id": "id2",
"details2": {
"data": [
{
"name": "name3",
"desc": "my desc3",
},
{
"name": "name4",
"desc": "my desc4",
}
]
}
}
]
}
}
and My java Code is:
JSONObject jsono = new JSONObject(data);
JSONObject mainObject = jsono.getJSONObject("main");
JSONArray jsonArray = mainObject.getJSONArray("details");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object1 = jsonArray.getJSONObject(i);
JSONObject objectDetails2 = object1.getJSONObject("details2");
JSONArray jsonArrayData = objectDetails2.getJSONArray("data");
for (int j = 0; j < jsonArrayData.length(); j++) {
JSONObject object = jsonArrayData.getJSONObject(j);
Actors actor = new Actors();
actor.setName(object.getString("name"));
actor.setDesc(object.getString("desc"));
actorsList.add(actor);
}
It only show First "data" result inside details2 not showing second details2 inside data..
Now my result is: "name1,dmy desc1,name2,my desc2"
i want all result like : "name1,dmy desc1,name2,my desc2,name3,my desc3,name4,my desc4"
How can i go again inside object and array? i need details from "name"
and "desc"
Get details2 from object JSONObject and then get data JSONArray:
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
// get details2 JSONObject
if(object.has("details2")){
if(!object.isNUll("details2")){
JSONObject objectDetails2 = object.getJSONObject("details2");
// get data JSONArray from objectDetails2
if(objectDetails2.has("data")){
if(!objectDetails2.isNUll("data")){
JSONArray jsonArrayData = objectDetails2.getJSONArray("data");
// iterate to jsonArrayData
for (int j = 0; j < jsonArrayData.length(); j++) {
JSONObject objectInner = jsonArray.getJSONObject(j);
String strName=objectInner.optString("name");
....
}
}else{
// details2 found but null
}
}else{
// details2 not found
}
}else{
// details2 found but null
}
}else{
// details2 not found
}
}
I think you should go into "details2" JSON Object like this :
JSONArray jsonArrayData=object.getJSONObject("details2").getJSONArray("data");
you have to get the object first and then parse it for the diffrent names and info
JSONArray jsonArrayData;
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject("details");
jsonArrayData=object.getJSONArray("data");
for(int k=0;k<jsonArrayData.length();k++)
{
Actors actor = new Actors();
actor.setName(object.getString("name"));
actor.setDesc(object.getString("desc"));
actorsList.add(actor);
}
}
i will suggest you use GSON it is quite simple to use,
I want to get only some selected values from the JSON response.For example if JSON contains 100 string values then i need to take the values which are starting with # symbol from that 100 values.
How can I do that ?
Following is my JSON,
[{"Obj" :
{ "ID":"11",
"NAME":"XYZ",
"GENDER":"M"
}
{ "ID":"11",
"NAME":"#XYZ",
"GENDER":"M"
}
{ "ID":"11",
"NAME":"#XYZ",
"GENDER":"M"
}
}]
Here I need to fetch Name which having # symbol
You can use below method to get the Name which start with # :
JSONObject jsonObject = new JSONObject(response);// u can change it as per your need
JSONArray jArray = jsonObject.getJSONArray("Obj");// if your `Obj` is an JsonArray
for (int i = 0; i < jArray.length(); i++) {
String json_name = jArray.getJSONObject(i).getString("NAME");
if(json_name.startsWith("#"))
{
Log.d(TAG,"It start with #");
}
}
Try out as below:
private void parseJSON(String json) {
try {
JSONArray items = new JSONArray(<your Json Response>);
for (int i = 0; i < items.length(); i++) {
JSONObject item = items.getJSONObject(i);
System.err.println("Object---" + item.getString("Obj"));
JSONObject obj=item.getJSONObject("Obj");
for (int j = 0; j < obj.length(); j++) {
String Name=obj.getString("NAME");
if(obj.getString("NAME").toString().contains("#"))
{
Log.d("Name starts with-->", Name);
}
else
{
Log.d("Name does not start with-->", Name);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
Try this.
first of all that's not a valid json may be below json is correct format
[
{
"Obj": [
{
"ID": "11",
"NAME": "XYZ",
"GENDER": "M"
},
{
"ID": "11",
"NAME": "#XYZ",
"GENDER": "M"
},
{
"ID": "11",
"NAME": "#XYZ",
"GENDER": "M"
}
]
}
]
if your response like above try below code this may help you.
try {
JSONArray jsoArray = new JSONArray(json);
JSONArray JobjArray = jsoArray.getJSONObject(0).getJSONArray("Obj");
for(int i=0; i < JobjArray.length(); i++)
{
JSONObject Jobj = JobjArray.getJSONObject(i);
Iterator<String> iter = Jobj.keys();
while (iter.hasNext()) {
String key = iter.next();
Log.v("key--", key);
try {
Object value = Jobj.get(key);
Log.v("value--", ""+value);
String str_value = value.toString().trim();
if(str_value.startsWith("#"))
{
Log.d(""+str_value,"value started with #");
}
} catch (JSONException e) {
// Something went wrong!
e.printStackTrace();
}
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i want to parse the json object using json library.
{
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" },
{ "id": "1003", "type": "Blueberry" },
{ "id": "1004", "type": "Devil's Food" }
]
}
}
Using JSON..
JSONObject object = new JSONObject(yourString);
JSONObject batters = object.getJSONObject("batters");
JSONArray batter = batters.getJSONArray("batter");
for(int i = 0 ; i < batter.length() ; i++) {
JSONObject object1 = (JSONObject) batter.get(i);
String id = object1.getString("id");
}
{
"result": "success",
"countryCodeList":
[
{"countryCode":"00","countryName":"World Wide"},
{"countryCode":"kr","countryName":"Korea"}
]
}
Here below I am fetching country details
JSONObject json = new JSONObject(jsonstring);
JSONArray nameArray = json.names();
JSONArray valArray = json.toJSONArray(nameArray);
JSONArray valArray1 = valArray.getJSONArray(1);
valArray1.toString().replace("[", "");
valArray1.toString().replace("]", "");
int len = valArray1.length();
for (int i = 0; i < valArray1.length(); i++) {
Country country = new Country();
JSONObject arr = valArray1.getJSONObject(i);
country.setCountryCode(arr.getString("countryCode"));
country.setCountryName(arr.getString("countryName"));
arrCountries.add(country);
}