nida nida - 1 month ago 6
Java Question

How to Save JSON in String variable using JAVA In Android?

i am newbie in android Here is JSON defined, i need to manually store this JSON in String variable using JAVA in Android.I am getting diffculty to do this.here is the code i have tried but its not correct.

String jsonObject = "{


{
"id": 22,
"menu_id": 1,
"field_type_id": 1,
"c4w_code": "1234",
"field_label": "Customer No",
"field_values": "",
"date_Created": "2012-09-16 05:11:23",
"date_modified": "2013-11-20 10:33:23",
"is_required": 0,
"is_static": 1,
"field_order": 1
},
{
"id": 23,
"menu_id": 1,
"field_type_id": 1,
"c4w_code": "1234",
"field_label": "Company Name",
"field_values": "",
"date_Created": "2012-09-16 05:11:56",
"date_modified": "2013-11-20 10:33:23",
"is_required": 1,
"is_static": 1,
"field_order": 3
}

}";


i need to store it either in String or in JSONObject so that i can move a head to work with this.
after adding code :

MenuFields menuFieldInstance[];// an array of bean Class
Gson gson = new Gson();
String jsonObjectString ="{\n\n\n {\n \"id\": 22,\n \"menu_id\": 1,\n \"field_type_id\": 1,\n \"c4w_code\": \"1234\",\n \"field_label\": \"Customer No\",\n \"field_values\": \"\",\n \"date_Created\": \"2012-09-16 05:11:23\",\n \"date_modified\": \"2013-11-20 10:33:23\",\n \"is_required\": 0,\n \"is_static\": 1,\n \"field_order\": 1\n },\n {\n \"id\": 23,\n \"menu_id\": 1,\n \"field_type_id\": 1,\n \"c4w_code\": \"1234\",\n \"field_label\": \"Company Name\",\n \"field_values\": \"\",\n \"date_Created\": \"2012-09-16 05:11:56\",\n \"date_modified\": \"2013-11-20 10:33:23\",\n \"is_required\": 1,\n \"is_static\": 1,\n \"field_order\": 3\n }\n\n}";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
menuFieldInstance = gson.fromJson(jsonObjectString,MenuFields[].class);
for(int i=0;i<jsonObjectString.length();i++)
{
Log.i("menuField Instance ",""+menuFieldInstance[i].getId());
}
}


i am getting this logCat:


12-30 22:56:47.745: E/AndroidRuntime(2238): FATAL EXCEPTION: AsyncTask #3
12-30 22:56:47.745: E/AndroidRuntime(2238): java.lang.RuntimeException: An error occured while executing doInBackground()
12-30 22:56:47.745: E/AndroidRuntime(2238): at android.os.AsyncTask$3.done(AsyncTask.java:299)
12-30 22:56:47.745: E/AndroidRuntime(2238): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
12-30 22:56:47.745: E/AndroidRuntime(2238): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-30 22:56:47.745: E/AndroidRuntime(2238): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
12-30 22:56:47.745: E/AndroidRuntime(2238): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-30 22:56:47.745: E/AndroidRuntime(2238): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-30 22:56:47.745: E/AndroidRuntime(2238): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-30 22:56:47.745: E/AndroidRuntime(2238): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-30 22:56:47.745: E/AndroidRuntime(2238): at java.lang.Thread.run(Thread.java:856)
12-30 22:56:47.745: E/AndroidRuntime(2238): Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2
12-30 22:56:47.745: E/AndroidRuntime(2238): at com.google.gson.Gson.fromJson(Gson.java:815)
12-30 22:56:47.745: E/AndroidRuntime(2238): at com.google.gson.Gson.fromJson(Gson.java:768)
12-30 22:56:47.745: E/AndroidRuntime(2238): at com.google.gson.Gson.fromJson(Gson.java:717)
12-30 22:56:47.745: E/AndroidRuntime(2238): at com.google.gson.Gson.fromJson(Gson.java:689)
12-30 22:56:47.745: E/AndroidRuntime(2238): at com.survey.management.activity.MainActivity$RequestForCustomerMenuFields.getCustomerMenuFieldsData(MainActivity.java:480)
12-30 22:56:47.745: E/AndroidRuntime(2238): at com.survey.management.activity.MainActivity$RequestForCustomerMenuFields.doInBackground(MainActivity.java:456)
12-30 22:56:47.745: E/AndroidRuntime(2238): at com.survey.management.activity.MainActivity$RequestForCustomerMenuFields.doInBackground(MainActivity.java:1)
12-30 22:56:47.745: E/AndroidRuntime(2238): at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-30 22:56:47.745: E/AndroidRuntime(2238): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-30 22:56:47.745: E/AndroidRuntime(2238): ... 5 more
12-30 22:56:47.745: E/AndroidRuntime(2238): Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2
12-30 22:56:47.745: E/AndroidRuntime(2238): at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:338)
12-30 22:56:47.745: E/AndroidRuntime(2238): at com.google.gson.internal.bind.ArrayTypeAdapter.read(ArrayTypeAdapter.java:70)
12-30 22:56:47.745: E/AndroidRuntime(2238): at com.google.gson.Gson.fromJson(Gson.java:803)
12-30 22:56:47.745: E/AndroidRuntime(2238): ... 13 more

Answer

You need to replace all your (double quote) " to \" to compile the String object in java.

Use this string:--

String jsonObject ="{\n\n\n {\n \"id\": 22,\n \"menu_id\": 1,\n \"field_type_id\": 1,\n \"c4w_code\": \"1234\",\n \"field_label\": \"Customer No\",\n \"field_values\": \"\",\n \"date_Created\": \"2012-09-16 05:11:23\",\n \"date_modified\": \"2013-11-20 10:33:23\",\n \"is_required\": 0,\n \"is_static\": 1,\n \"field_order\": 1\n },\n {\n \"id\": 23,\n \"menu_id\": 1,\n \"field_type_id\": 1,\n \"c4w_code\": \"1234\",\n \"field_label\": \"Company Name\",\n \"field_values\": \"\",\n \"date_Created\": \"2012-09-16 05:11:56\",\n \"date_modified\": \"2013-11-20 10:33:23\",\n \"is_required\": 1,\n \"is_static\": 1,\n \"field_order\": 3\n }\n\n}";