Posted By

variable3 on 05/09/11


Tagged

json android


Versions (?)

android string to json object conversion


 / Published in: Java
 

This code is used for converting string to json object

  1. //MainActivity.java
  2. //====================
  3. package com.v3;
  4.  
  5. import org.json.JSONObject;
  6.  
  7. import android.app.Activity;
  8. import android.os.Bundle;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.EditText;
  13. import android.widget.TextView;
  14.  
  15. public class MainActivity extends Activity {
  16. TextView Tname,TCountry,TDob,TCity;
  17. EditText Ename,ECountry,EDob,ECity;
  18. Button btnCreate;
  19. String data="";
  20. /** Called when the activity is first created. */
  21. @Override
  22. public void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.main);
  25. btnCreate = (Button) findViewById(R.id.btnGen);
  26. Tname = (TextView) findViewById(R.id.txtName);
  27. Ename = (EditText) findViewById(R.id.editName);
  28. TCity = (TextView) findViewById(R.id.txtCity);
  29. ECity = (EditText) findViewById(R.id.editCity);
  30. TCountry = (TextView) findViewById(R.id.txtCountry);
  31. ECountry = (EditText) findViewById(R.id.editCountry);
  32. TDob = (TextView) findViewById(R.id.txtDob);
  33. EDob = (EditText) findViewById(R.id.editDob);
  34.  
  35. btnCreate.setOnClickListener(new Button.OnClickListener()
  36. {
  37. public void onClick(View v)
  38. {
  39. examineJSONFile();
  40. }
  41. });
  42. }
  43.  
  44. void examineJSONFile()
  45. {
  46. try
  47. {
  48. JSONObject object=new JSONObject();
  49. object.put("Name", Ename.getText());
  50. object.put("City", ECity.getText());
  51. object.put("Country", ECountry.getText());
  52. object.put("Dob", EDob.getText());
  53. Log.i("JsonString :", object.toString());
  54.  
  55. }
  56. catch (Exception je)
  57. {
  58.  
  59. }
  60. }
  61. }
  62. //main.xml
  63. //============
  64. <?xml version="1.0" encoding="utf-8"?>
  65. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  66. android:orientation="horizontal"
  67. android:layout_width="fill_parent"
  68. android:layout_height="fill_parent"
  69. >
  70. <TableLayout android:id="@+id/tableLayout1"
  71. android:layout_width="match_parent"
  72. android:layout_height="wrap_content"
  73. android:stretchColumns="1">
  74. <TableRow >
  75. <TextView
  76. android:text="Name"
  77. android:id="@+id/txtName"
  78. android:layout_width="wrap_content"
  79. android:layout_height="wrap_content"/>
  80. <EditText android:text="" android:id="@+id/editName"
  81. android:layout_width="fill_parent"
  82. android:layout_height="wrap_content"/>
  83. </TableRow>
  84. <TableRow>
  85. <TextView
  86. android:text="City"
  87. android:id="@+id/txtCity"
  88. android:layout_width="wrap_content"
  89. android:layout_height="wrap_content"/>
  90. <EditText android:text="" android:id="@+id/editCity"
  91. android:layout_width="fill_parent"
  92. android:layout_height="wrap_content"/>
  93. </TableRow>
  94. <TableRow >
  95. <TextView
  96. android:text="Country :"
  97. android:id="@+id/txtCountry"
  98. android:layout_width="wrap_content"
  99. android:layout_height="wrap_content"/>
  100. <EditText android:text="" android:id="@+id/editCountry"
  101. android:layout_width="fill_parent"
  102. android:layout_height="wrap_content"/>
  103. </TableRow>
  104. <TableRow >
  105. <TextView
  106. android:text="DOB"
  107. android:id="@+id/txtDob"
  108. android:layout_width="wrap_content"
  109. android:layout_height="wrap_content"/>
  110. <EditText android:text="" android:id="@+id/editDob"
  111. android:layout_width="fill_parent"
  112. android:layout_height="wrap_content"/>
  113.  
  114. </TableRow>
  115. <TableRow >
  116. <Button android:text="Generate"
  117. android:id="@+id/btnGen"
  118. android:layout_width="wrap_content"
  119. android:layout_height="wrap_content"/>
  120.  
  121. </TableRow>
  122. </TableLayout>
  123.  
  124. </LinearLayout>

Report this snippet  

You need to login to post a comment.