I want to send Json Parameters (below) in Android - POST Method.
{"message":"This is venkatesh","visit":[5,1,2]}
I tried the below code
String IDs="5,1,2";
JSONObject jsonObject = new JSONObject();
jsonObject.put("message", "This is venkatesh");
JSONArray jsonArray = new JSONArray();
jsonArray.put(IDs);
jsonObject.put("visit", jsonArray);
String json = jsonObject.toString();
Log.d("Mainactivity", " json" + json);
I am getting the output is
{"message":"This is venkatesh","visit":["5,1,2"]}
// Output i am get with double quotes inside visit
{"message":"This is venkatesh","visit":[5,1,2]}
// I want to send this parameter without Double quotes inside the Visit
String IDs="5,1,2";
String[] numbers = IDs.split(",");
JSONArray jsonArray = new JSONArray();
for(int i = 0; i < numbers.length(); i++)
{
jsonArray.put(Integer.parseInt(numbers[i]));
}
Hope this helps.
In array add it as integer not as a String
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("message", "This is venkatesh");
JSONArray jsonArray = new JSONArray();
jsonArray.put(5);
jsonArray.put(1);
jsonArray.put(2);
jsonObject.put("visit", jsonArray);
String json = jsonObject.toString();
Log.i("TAG", " json" + json); //{"message":"This is venkatesh","visit":[5,1,2]}
} catch (JSONException e) {
e.printStackTrace();
}
Just replace following line:
jsonArray.put(IDs);
with following code:
jsonArray.put(5);
jsonArray.put(1);
jsonArray.put(2);
So you should use 'int' values if you want to see array without quotes. The point is 'quotes' means that this is String object. Proof is following line of your code:
String IDs="5,1,2";
JSONObject jsonObject = new JSONObject();
jsonObject.put("message", "This is venkatesh");
JSONArray jsonArray = new JSONArray(new int[](5, 1, 2));
jsonObject.put("visit", jsonArray);
I am assuming you will convert the String to integer array and after you do this is how you can add,
Well the only difference you need to understand is that, JSON adds double quotes for values of String and not of Integer.
so for Key value pair for String it would be
"key":"value"
so for Key value pair for Integer it would be
"key":123
so for Key value pair for boolean it would be
"key":true
With that knowledge you can edit your code.
Code
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put("message", "This is venkatesh");
JSONArray jsonArray = new JSONArray();
jsonArray.put(0,5);
jsonArray.put(1,1);
jsonArray.put(2,2);
jsonObject.put("visit", jsonArray);
Log.d("TAG","result "+jsonObject.toString());
} catch (Exception e) {
e.printStackTrace();
}
Output
{"message":"This is venkatesh","visit":[5,1,2]}
int[] arrayOfInteger=[1,2,3];
JSONObject jsonObject =new JSONObject();
jsonObject .put("message","your message");
JSONArray jsonArray = new JSONArray(arrayOfInteger);
jsonObject .put("visit",jsonArray );
Result : {"message":"your message","visit":[1,2,3]}
Related
{"721xxxxxxx":true,"722xxxxxxx":true,"723xxxxxxx":true}
How can i read all the keys in the json and how can count all the data in the Jsonarray
My code-->
String members = s;
/* s contain {"721xxxxxxx":true,"722xxxxxxx":true,"723xxxxxxx":true} */
JSONArray jsonChildArr = new JSONArray(members);
Log.e("Error","Data in jsonArray--->"+jsonChildArr.length());
Simple Way to Read JSON Object
JSONObject jsonObject = new JSONObject(members);
Log.i("Data","Data in jsonObject--->"+jsonObject.getBoolean("721xxxxxxx"));
Do Like this.
try {
String members = s;
/* s contain {"721xxxxxxx":true,"722xxxxxxx":true,"723xxxxxxx":true} */
JSONObject obj = new JSONObject(members);
JSONArray array = obj.names();
Log.d("ItemCount",array.length()+"");
for(int i=0;i<array.length();i++){
Log.d("Item",array.getString(i)+":"+ obj.getBoolean(array.getString(i));
}
} catch (JSONException e) {
e.printStackTrace();
}
Try this,
JSONObject jsonObject = new JSONObject(result);
boolean data = jsonObject .getBoolean("721xxxxxxx");
Log.d("Data","data:"+data );
try this:
String jsonStr = "{"721xxxxxxx":true,"722xxxxxxx":true,"723xxxxxxx":true}";
JSONObject itemJsonObj = new JSONObject(jsonStr );
HashMap<String,Boolean> result = new HashMap<>();
while(itemJsonObj.keys().hasNext()){
//parse json and save as key-value.
result.put(itemJsonObj.keys().next()+"",itemJsonObj.get(itemJsonObj.keys().next());
// result.size() is like array's size.
}
I wrote a library for parsing and generating JSON in Android http://github.com/amirdew/JSON
for your sample:
JSON jsonData = new JSON(jsonString);
int count = jsonData.count();
I have request something like this.
{"REQ_DATA":
{"CLPH_NO":"010123456789","USE_INTT_NO":""}
}
but server accepts only this
{"REQ_DATA":
[{"CLPH_NO":"010123456789","USE_INTT_NO":""}]
}
What should I do? I quite noob about JSON, please help me.
REQ_DATA needs to be a JSONArray. Try implementing something like this
try {
JSONObject object = new JSONObject();
JSONArray requiredDataArray = new JSONArray();
JSONObject data = new JSONObject();
data.put("CLPH_NO", "010123456789");
data.put("USE_INTT_NO", "");
requiredDataArray.put(data);
object.put("REQ_DATA", requiredDataArray);
Log.d("JSON", object.toString());
} catch (JSONException e) {
e.printStackTrace();
}
You can try:
JSONObject fromRequest = request.getJSONObject();// given from request
JSONObject toServer = new JSONObject();
JSONArray arr = new JSONArray();
arr.put(fromRequest.get("REQ_DATA"));
toServer.put("REQ_DATA", arr);
Jsonarray array = new Jsonarray();
Jsonobject insidearray = new jsonobject;
insidearray.put("CLPH_NO", "010123456789");
insidearray.put("USE_INTT_NO", "");
array.put(insidearray);
Jsonobject object = new Jsonobject();
object.put("REQ_DATA",array);
The variables may need a little editing, doing this from a phone but there you go
You don't change a JSONObject to a JSONArray, rather, you create a JSONArray and then add the JSONObject to that array.
try {
JSONObject reqData = new JSONObject();
reqData.put("CLPH_NO", "010123456789");
reqData.put("USE_INTT_NO", "");
JSONArray array = new JSONArray();
array.put(reqData);
JSONObject request = new JSONObject();
request.put("REQ_DATA", reqData);
String requestAsJSONString = request.toString();
// call web service
} catch (JSONException e) {
// handle exception
}
You need to send requestAsJSONString to the server.
Furthermore, I suggest you put the JSON object keys in final fields, like so:
static final String KEY_REQ_DATA = "REQ_DATA";
and then use KEY_REQ_DATA in your code instead of using the hardcoded String.
This is simple solution for your json string as per server check it
JSONObject jo = new JSONObject();
try {
jo.put("CLPH_NO", "010123456789");
jo.put("USE_INTT_NO", "");
} catch (Exception e) {
}
JSONArray ja = new JSONArray();
ja.put(jo);
JSONObject final_jo = new JSONObject();
try {
final_jo.put("REQ_DATA", ja);
} catch (Exception e) {
}
Toast.makeText(getApplicationContext(),final_jo.toString(),Toast.LENGTH_LONG).show();
Just simple solution without using any hard-coded Strings
JSONObject currentJson = new JSONObject(yourJsonString); //Your current jsonObject
JSONObject newJsonObject = new JSONObject(); //new jsonObject you want to send to server
newJsonObject.put(currentJson.keys().next().toString(),
new JSONArray().put(currentJson.getString(currentJson.keys().next().toString())));
My application connects to the web and retrieves a JSON file. I am having some issues retrieving the data that I need from this file. Here is the link to the JSON file: http://api.wunderground.com/api/f9d9bc3cc3834375/forecast/q/CA/San_Francisco.json
Here is a snippet of what it looks like:
I would like to get the value of the "period" variable in the first JSon object within the "forecastday" array, which should be 0. Here is how i'm looking at this. "forecastday" is the array, within that there are a number of JSon objects, each containing variables like "period","icon"....."pop".
In my code, I attempt to get the JSON array "forecastday" and then get the first Json object of the array, I then retrieve the value of "period" in that object and set it in a TextView:
protected void onPostExecute(String response) {
if(response == null) {
response = "THERE WAS AN ERROR";
}
progressBar.setVisibility(View.GONE);
try {
JSONObject jsonObj = new JSONObject(response);
JSONArray contacts = jsonObj.getJSONArray("forecastday");
JSONObject c = contacts.getJSONObject(0);
String period = c.getString("period");
responseView.setText(period);
} catch (JSONException e) {
e.printStackTrace();
}
}
When I run the code, nothing is being retrieved. I'm new to working with JSon and am wondering if I'm looking at this wrong. Please help.
Your "forecastday" JSONArray is inside the "txt_forecast" JSONObject that is inside the "forecast" JSONObject of your response, so you have to extract your JSONArray from this JSONObject and not from the root JSON response :
try {
JSONObject jsonObj = new JSONObject(response);
--> JSONObject forecatsObj = jsonObj.getJSONObject("forecast");
--> JSONObject txtForecatsObj = forecatsObj.getJSONObject("txt_forecast");
JSONArray contacts = txtForecatsObj.getJSONArray("forecastday");
...
You were close but it is incorrect, try like this:
JSONObject jsonObj = new JSONObject(response);
JSONObject forecast = jsonObj.getJSONObject("forecast");
JSONObject txtForecast = forecast.getJSONObject("txt_forecast");
JSONArray forecastDay = txtForecast.getJSONArray("forecastday");
//parse the first period value
String period = forecastDay.getJSONObject(0).getString("period");
responseView.setText(period);
Try this code :
try {
JSONObject jsonObj = new JSONObject(response);
JSONObject forecastObj = jsonObj.getJSONObject("forecast");
JSONObject txt_forecastObj = forecastObj.getJSONObject("txt_forecast");
JSONArray foracastdayArray = txt_forecastObj.getJSONArray("foracastday");
JSONObject oOjb0 = foracastdayArray.getJSONObject(0);
String period = oOjb0.getString("perioed");
}catch (Exception e){
}
This code works for me fine:
JSONObject jsonResponse = new JSONObject(responce);
JSONArray jsonMainNode = jsonResponse.optJSONArray("forecastday");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String period = jsonChildNode.optString("period");
responceView.setText(period);
}
I want to return this back to my android emulator. This is a combination of jsonArray and jsonObject
[{"id":"11WAD01442","name":"Teng Kwang Wei"},{"id":"11WAD01443","name":"test 1"},{"id":"11WAD01444","name":"test 2"},{"id":"11WAD01445","name":"test 3"},{"status":true}]
What should I use to receive this json encode.
JSONArray jsonArray = new JSONArray(content);
or
JSONObject jsonObject = new JSONObject(content);
You need to decode the top object. In your case is an Array (of JSONObjects):
JSONArray jsonArray = new JSONArray(content);
for (int i=0, len=jsonArray.length(); i<len; i++) try {
JSONObject obj=jsonArray.getJSONObject(i);
String id=obj.getString("id");
String name=obj.getString("name");
.
.
Log.d("DUMP", "User "+i+": id="+id+", name="+name);
} catch (JSONException e) {
Log.d("DUMP", "Problem with user");
}
l get string with answer from server. l want to do it JSONObject.
l do
JSONObject jsonObj = new JSONObject(json);
in json has
"{"sentences":[{"trans":"R\u0455R\u0491ReR\u0405","orig":"�\u0455�\u0491��\u0405","translit":"","src_translit":"R\u1E91Rg\u0300RoR\u1E90"}],"src":"ru","server_time":1}"
but jsonObj has
"{"sentences":[{"src_translit":"RẑRg̀RoRẐ","orig":"�ѕ�ґ��Ѕ","trans":"RѕRґReRЅ","translit":""}],"server_time":1,"src":"ru"}"
so how l can get value from "trans":"RѕRґReRЅ"?
PS RѕRґReRЅ it's "own" in normal charset
If you already have the jsonObj you just have to do:
try {
// Getting Array of Sentences
sentences = json.getJSONArray("sentences");
// looping through All Contacts
for(int i = 0; i < sentences.length(); i++){
JSONObject c = sentences.getJSONObject(i);
// get the value from trans
String trans = c.getString("trans");
//now you should save this string in an array
}
} catch (JSONException e) {
e.printStackTrace();
}
try this
JSONObject jsonObj = new JSONObject(json);
JSONArray sentences = jsonObj.getJSONArray("sentences");
for(int i=0;i<sentences.length();i++){
JSONObject number = sentences.getJSONObject(i);
String transValue = number.getString("trans");
}
First you have to remove single quote form starting and from ending of your JSON string (json), by this your JSON will be a valid json for this you have to do like :
json= json.substring(1, json.length()-1);
After that you do like this :
JSONObject oJsonObject = new JSONObject(json);
JSONArray oJsonArray = oJsonObject.getJSONArray("sentences");
for(int i=0; i<oJsonArray.length(); i++)
{
JSONObject oJsonObject1 = oJsonArray.getJSONObject(i);
String transValue = oJsonObject1 .getString("trans");
}