Sending and Parsing Json Objects in Android

Sending and Parsing JSON Objects in Android

I am surprised these have not been mentioned: but instead of using bare-bones rather manual process with json.org's little package, GSon and Jackson are much more convenient to use. So:

  • GSON
  • Jackson

So you can actually bind to your own POJOs, not some half-assed tree nodes or Lists and Maps.
(and at least Jackson allows binding to such things too (perhaps GSON as well, not sure), JsonNode, Map, List, if you really want these instead of 'real' objects)

EDIT 19-MAR-2014:

Another new contender is Jackson jr library: it uses same fast Streaming parser/generator as Jackson (jackson-core), but data-binding part is tiny (50kB). Functionality is more limited (no annotations, just regular Java Beans), but performance-wise should be fast, and initialization (first-call) overhead very low as well.
So it just might be good choice, especially for smaller apps.

How to send and receive the json object from one activity to another?

You are doing the correct way. To get it in another activity, you can proceed as

if (getIntent().getExtras() != null) {
String scamDatas = getIntent().getStringExtra("scamDatas");
String scamSubCategoryText = getIntent().getStringExtra("scamSubCategoryText");
try {
JsonParser parser = new JsonParser();
JsonObject scamDataJsonObject = parser.parse(scamDatas).getAsJsonObject();
} catch (Exception e) {
e.printStackTrace();
}
}

how to parse this json in android with json library?

            JSONObject jsonObject = new JSONObject( JSON_response );
int status = jsonObject.getInt("status");
String message = jsonObject.getString("message");
String result = jsonObject.getString("result");
JSONObject jsonObject1 = new JSONObject( result );
int postBackParamId = jsonObject1.getInt("postBackParamId");
String mihpayid = jsonObject1.getString("mihpayid");
int paymentId = jsonObject1.getInt("paymentId");
String mode = jsonObject1.getString("mode");
String result_status = jsonObject1.getString("status");
String unmappedstatus = jsonObject1.getString("unmappedstatus");

How to Parse from the JSON file?

{ -> json object 
"result":"ok",
"numbers":[-> json array
{

Do like this

JSONObject jobject=new JSONObject(jsonString);
JSONArray jarray=Jobject.getJSONArray("numbers");
String result=jobject.getJSONObject("result");
for(int i=0;jarray.length();i++){
String first= jarray.getJSONObject(i).getString("First");
String Second= jarray.getJSONObject(i).getString("Second");
}

Android json parsing object inside another object and array

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
}
}

How to parse this Json Array in android

You BufferReader is not working as expected in your class JSONfunctions , do some changes in your class and it should looks like

JSONfunctions.java

public class JSONfunctions {

public static JSONArray getJSONfromURL(String url) {
InputStream is = null;
String result = "";
JSONArray jArray = null;

// Download JSON data from URL
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();

} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}


StringBuffer sb = new StringBuffer("");
try {
URL urls = new URL(url);
URLConnection urlConnection;
urlConnection = urls.openConnection();
InputStream in = urlConnection.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = br.readLine()) != null) {
sb.append(line);
}
System.out.println(sb);
result=sb.toString();
} catch (IOException e) {}
catch (Exception e) {}
try {

jArray = new JSONArray(result);
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}

return jArray;
}
}

and to avoid Exception check if JSONArray is not null then only it goes to run for loop like this :

 JSONArray ja2 =   JSONfunctions.getJSONfromURL("http://app.ireff.in:9090/IreffWeb/android?  service=airtel&circle=assam");
if(ja2!=null)
for (int i = 0; i < ja2.length(); i++) {}

How to parse JSON with Volley?

Try this:

    String url = "http://api.giphy.com/v1/gifs/search?q=funny+cat&api_key=dc6zaTOxFJmzC";
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jo = jsonArray.getJSONObject(i);
// Do you fancy stuff
// Example: String gifUrl = jo.getString("url");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Anything you want
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
requestQueue.add(stringRequest);

Parse Json Object in android?

Change this

 JSONObject mapDetails =        
jsonArryDetails.getJSONObject(0);

to

 JSONObject mapDetails =        
jsonArryDetails.getJSONObject(i);

You have to add something like

   ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

for(int i = 0;i<jsonArryDetails.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject mapDetails =
jsonArryDetails.getJSONObject(i);

lat1 = mapDetails.getString(LAT);
lng1 = mapDetails.getString(LNG);
address1 = mapDetails.getString(ADDRESS);
time1 = mapDetails.getString(CTIME);
map.put(LAT, lat1);
map.put(LNG, lg1);
map.put(ADDRESS, address1 );
map.put(CTIME, time1 );

mylist.add(map);

}


Related Topics



Leave a reply



Submit