Showing posts with label Json parsing. Show all posts
Showing posts with label Json parsing. Show all posts

Monday, August 22, 2011

Android and JSON

Here is basic JSON string which you will extract,Consider a string


{Question: {Q_id:111,Q_Name:How much is ur salry, TypeOfQuest: {Type:CheckBox,Values:[<50,>50,=50]}}}

The present string needs to be converted in to JSON form for doing this you can use String Builder Object else tou can convert manually
as below

Str="{\"Question\": {\"Q_id\":\"111\",\"Q_Name\":\"How much is ur salry\", \"TypeOfQuest\": {\"Type\":\"CheckBox\", " +
"\"Values\":[\"<50\",\">50\",\"=50\"]}}}";

Now is the Extracting of the JSON String

jObject = new JSONObject(Str);
  JSONObject Question = jObject.getJSONObject("Question");
  Q_id = Question.getString("Q_id");
  System.out.println(Q_id);

      Q_Name = Question.getString("Q_Name");
  System.out.println(Q_Name);

  JSONObject TypeOfQuest = Question.getJSONObject("TypeOfQuest");
  Type = TypeOfQuest.getString("Type");
  System.out.println(Type);
  ValuesArray = TypeOfQuest.getJSONArray("Values");
  //System.out.println(ValuesArray.length());

  for (int i = 0; i < ValuesArray.length(); i++) {
   //System.out.println(ValuesArray.getJSONArray(0).toString());
   System.out.println(ValuesArray.getString(i).toString());
} 


That is it..........................