Thursday, 25 May 2017

Creating JSON String using String and Backslash - Without using JSON Classes

1. Construct JsonObject String

String jsonObjectString = "{}";

show output in Log
Log.e("json object String", jsonObjectString);

Output
************************************
{  

}
************************************

2. Construct JsonArray String

String jsonArrayString = "[]";

show output in Log
Log.e("json Array String", jsonArrayString);

Output
************************************
[  

]
************************************

3. Construct and Insert static value into jsonObjectString

JSONObject string allow data into key,value pair

If you want to store value we need to set key for value.

Key or tag_name is a first part, value is second part

Insert static tag or key into jsonString use \"tag_name"\

Syntax:
String syntaxObjectString = "{\"tag_name\":\"value\"}";

trying to insert name(key or tag_name),venkat(value)
String jsonObjectString1 = "{\"name\":\"venkat\"}";

trying to insert phno(key or tag_name),0123456789(value)
String jsonObjectString2 = "{\"phno\":\"0123456789\"}";

trying to insert city(key or tag_name),Hyd(value)
String jsonObjectString3 = "{\"city\":\"Hyd\"}";

show output in Log
Log.e("jsonObjectString1", jsonObjectString1);
Log.e("jsonObjectString2", jsonObjectString2);
Log.e("jsonObjectString3", jsonObjectString3);

Output
************************************
{  
   
"name":"venkat"
}

{  
   
"phno":"0123456789"
}


{  
   
"city":"Hyd"
}
************************************

4. Construct and Insert static value into jsonArrayString

JSONArray string allow data into array format

We have chance to store values directly

Use this format to store value into jsonString \"value"\

Syntax:
String syntaxObjectString = "[\"value\"]";

trying to insert my name
String jsonArrayString1 = "[\"venkat\"]";

trying to insert my phno
String jsonArrayString2 = "[\"0123456789\"]";

trying to insert my city
String jsonArrayString3 = "[\"Hyd\"]";

show output in Log
Log.e("jsonArrayString1", jsonArrayString1);
Log.e("jsonArrayString2", jsonArrayString2);
Log.e("jsonArrayString3", jsonArrayString3);

Output 1
************************************
[  
   "venkat"
]

[  
   "0123456789"

]

[  
   "Hyd"

]
************************************

5. If you want insert multiple strings in one Json Object String

Syntax:
String syntaxString = "{\"tag_name1\":\"value1\",\"tag_name2\":\"value2\"}";

String jsonString = "{\"name\":\"venkat\",\"phno\":\"0123456789\"}";

show output in Log
Log.e("jsonString", jsonString);

Output
************************************
{  
   
"name":"venkat",
   
"phno":"0123456789"
}
************************************

6. If you want insert multiple strings in one json array string

Syntax:
String syntaxString = "[\"value1\",\"value2\"]";

String jsonString = "[\"venkat\",\"0123456789\"]";

show output in Log
Log.e("jsonString", jsonString);

Output
************************************
[  
   "venkat"
,
   "0123456789"
]
************************************

7. If you want insert JSONObjectString into another JSONObjectString

String syntax = "{\"tag_name1\":{\"tag_name2\":\"value2\"}}";

String jsonString = "{\"innerobject\":{\"name\":\"venkat\"}}";

show output in Log
Log.e("jsonString", jsonString);

Output
************************************
{  
   
"innerobject":{  
      
"name":"venkat"
   
}
}
************************************

8. If you want insert Json array string into Json Object string

String syntax = "{\"tag_name1\":[\"value2\"]}";
                            
String jsonString = "{\"innerarray\":[\"venkat\"]}";
                            
show output in Log
Log.e("jsonString", jsonString);

Output
************************************
{  
   
"innerarray":[  
      "venkat"
   
]
}
************************************

9. If you want insert Json object string into Json array string

String syntax = "[{\"tag_name\":\"value\"}]";

String jsonString = "[{\"name\":\"venkat\"}]";

show output in Log
Log.e("jsonString", jsonString);

Output
************************************
[  
   
{  
      
"name":"venkat"
   
}
]
************************************

10. If you want insert Json array string into Json array string

String syntax = "[[\"value\"]]";

String jsonString = "[[\"venkat\"]]";

show output in Log
Log.e("jsonString", jsonString);

Output
************************************
[  
   
[  
      "venkat"
   
]
]
************************************

11. If you want insert dynamic values in Json object string

EditText edt1 = (EditText) findViewById(R.id.edt1);

String dynamic_value = edt1.getText().toString();

String syntax = "{\"tag_name\":\"" + dynamic_value + "\"}";

String jsonString = "{\"name\":\"" + dynamic_value + "\"}";

show output in Log
Log.e("jsonString", jsonString);

Output
************************************
{  
   
"name":"Ganesh"
}
************************************

12. If you want insert multiple dynamic values in Json object string


EditText edt1 = (EditText) findViewById(R.id.edt1);
EditText edt2 = (EditText) findViewById(R.id.edt2);
EditText edt3 = (EditText) findViewById(R.id.edt3);

Fetching dynamic values from edittexts
String dynamic_value1 = edt1.getText().toString();
String dynamic_value2 = edt2.getText().toString();
String dynamic_value3 = edt3.getText().toString();

String syntax = "{\"tag_name1\":\"" + dynamic_value1+ "\",\"tag_name2\":\"" +dynamic_value2+ "\",\"tag_name3\":\"" + dynamic_value3 + "\"}";

String jsonString = "{\"name\":\"" + dynamic_value1+ "\",\"phone\":\"" + dynamic_value2 + "\",\"city\":\""+ dynamic_value3 + "\"}";

show output in Log
Log.e("jsonString", jsonString);

Output
************************************
{  
   
"name":"Ganesh",
   
"phone":"1234567890",
   
"city":"Mas"
}
************************************

13. If you want insert dynamic values in Json array string

EditText edt1 = (EditText) findViewById(R.id.edt1);

String dynamic_value = edt1.getText().toString();

String syntax = "[\"" + dynamic_value + "\"]";

String jsonString = "[\"" + dynamic_value + "\"]";

show output in Log
Log.e("jsonString", jsonString);

Output
************************************
[  
   "Ganesh"
]
************************************

14. If you want insert multiple dynamic values in Json Array string

EditText edt1 = (EditText) findViewById(R.id.edt1);
EditText edt2 = (EditText) findViewById(R.id.edt2);
EditText edt3 = (EditText) findViewById(R.id.edt3);

Fetching dynamic values from edittexts
String dynamic_value1 = edt1.getText().toString();
String dynamic_value2 = edt2.getText().toString();
String dynamic_value3 = edt3.getText().toString();

String syntax = "[\"" + dynamic_value1 + "\",\""+dynamic_value2+ "\",\"" + dynamic_value3 + "\"]";

String jsonString = "[\"" + dynamic_value1 + "\",\""+dynamic_value2 + "\",\"" + dynamic_value3 + "\"]";

// show output in Log
Log.e("jsonString", jsonString);

Output
************************************
[  
   "Ganesh"
,
   "1234567890"
,
   "Mas"
]
************************************


No comments:

Post a Comment