How to parse HTTP response ton JSON object ?
I changed my json code to:
{
"data": [{
"id": 1,
"sensor1": 76,
"sensor2": 75
}, {
"id": 2,
"sensor1": 76,
"sensor2": 206
}]
}
And my code is now:
JSONObject json = new JSONObject(mJson);
try {
data = json.getJSONArray("data");
for (int i = 0; i < json.length(); i++) {
HashMap<String, Integer> map = new HashMap<String, Integer>();
JSONObject obj = (JSONObject) data.get(i);
map.put(TAG_SENSOR_1, obj.getInt("sensor1"));
map.put(TAG_SENSOR_2, obj.getInt("sensor2"));
dataList.add(data.getInt("id") - 1, map);
}
} catch (JSONException e) {
e.printStackTrace();
}
But I have the same problem
edit your string to this format
{content:[{
first:1,
second:76,
third:75
},
{
first:2,
second:76,
third:75
},
{
first:3,
second:206,
third:75
},
{
first:4,
second:6,
third:175
},
{
first:5,
second:176,
third:75
}]
}
then use JSONObject and JSONArray to convert string
JSONArray array = new JSONObject(string).getJSONArray("content");
for(int i=0;i<array.size();i++){
JSONObject obj = array.get(i);
Log.i(TAG,"obj.getInt = "+obj.getInt("first"));
Log.i(TAG,"obj.getInt = "+obj.getInt("second"));
Log.i(TAG,"obj.getInt = "+obj.getInt("third"));
}
Related
How to parse JSON values in this format? I want to get the details of the data element but inside data there are 'dates' and inside dates there is array containing two more elements. I want to get all the dates first inside data and then within these dates I want all the information within these dates. How can I achieve this? Please Help. I tried with below code but it hasn't worked
try {
JSONObject jsonObject = new JSONObject("data");
JSONArray jsonArray =jsonObject.getJSONArray(String.valueof(cuurentdate));
JSONArray session;
for (int i = 0; i < jsonArray.length() - 1; i++) {
jsonObject = jsonArray.getJSONObject(i);
session= jsonObject.getJSONArray("session");
Log.d("MyLog", session + "");
}
} catch (JSONException e) {
e.printStackTrace();
}
Following is the format
{
"status": 1,
"status_code": 200,
"data": {
"2018-02-11": [
{
"session": "01:00 AM",
"place": true
},
{
"session": "02:00 AM",
"place": true
}
],
"2018-02-12": [
{
"session": "01:00 AM",
"place": true
},
{
"session": "02:00 AM",
"place": true
}
]
}
}
You just need to pass the response string to the method. You can try this:
private void jsonParsing(String jsonString) {
// String jsonString = "{ \"status\": 1, \"status_code\": 200, \"data\": { \"2018-02-11\": [ { \"session\": \"01:00 AM\", \"place\": true }, { \"session\": \"02:00 AM\", \"place\": true } ], \"2018-02-12\": [ { \"session\": \"01:00 AM\", \"place\": true }, { \"session\": \"02:00 AM\", \"place\": true } ] } }";
try {
JSONObject jsonObject = new JSONObject(jsonString);
JSONObject dataObj = jsonObject.getJSONObject("data");
Iterator<String> iter = dataObj.keys();
Log.e(TAG, "jsonParsing: "+iter );
while (iter.hasNext()) {
String key = iter.next();
JSONArray datesArray = dataObj.getJSONArray(key);
ArrayList<String> sessions = new ArrayList<String>();
for (int i = 0; i < datesArray.length(); i++) {
JSONObject datesObject = datesArray.getJSONObject(i);
sessions.add(datesObject.getString("session"));
}
Log.d("MyLog", sessions + "");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
(1) get JSONObject of Main json
JSONObject objMain = new JSONObject("your json string");
(2)get JSONObject of "data" from main json
JSONObject jsonData = objMain.getJSONObject("data")
(3) get all keys (dates) from object "data"
Iterator<String> iter = jsonData.keys();
while (iter.hasNext()) {
String key = iter.next();
try {
JSONArray arrayDate = objData.getJSONArray(key)
for (i = 0; i < arrayDate.length(); i++) {
JSONObject objDate = arrayDate.getJSONObject(i)
Log.d("#session :", "" + objDate.getString("session"))
Log.d("#place :", "" + objDate.getBoolean("place"))
}
} catch (JSONException e) {
// Something went wrong!
}
}
try this one code
IN THIS CODE jsonMstObject IS TEMP OBJECT YOU HAVE TO USE YOUR API RESPONSE JSONobject INSTEAD OF jsonMstObject
try {
JSONObject jsonMstObject = new JSONObject("{"status":1,"status_code":200,"data":{"2018-02-11":[{"session":"01:00 AM","place":true},{"session":"02:00 AM","place":true}],"2018-02-12":[{"session":"01:00 AM","place":true},{"session":"02:00 AM","place":true}]}}");
JSONObject jsonObject = jsonMstObject.getJSONObject("data");
JSONArray jsonArray =jsonObject.getJSONArray(String.valueof(cuurentdate));
ArrayList<String> arrSession = new ArrayList<String>();
for (int i = 0; i < jsonArray.length(); i++) {
jsonObject = jsonArray.getJSONObject(i);
arrSession.add(jsonObject.getString("session"));
}
Log.d("MyLog", arrSession + "");
} catch (JSONException e) {
e.printStackTrace();
}
in this code arrSession is your session string array
Ex. you passed cuurentdate = "2018-02-11" then you recived result like
[01:00 AM, 02:00 AM]
Note: this code is worked based on your cuurentdate param. This is code for get static date array from data and create String Array.
I have two items in arraylist
Position 0 : product_id=500,amout=1
Position 1 : product_id=501,amout=1
I want to create this type of json
{
"user_id": "3",
"shipping_id": "1",
"payment_id": "2",
"products": {
"500": {
"product_id": "500",
"amount": "1"
},
"501": {
"product_id": "501",
"amount":"1"
}
}
}
This is my code
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("user_id", usr);
jsonObject.accumulate("shipping_id", shipping_id);
jsonObject.accumulate("payment_id", payment_id);
for( i=0;i<alProductss.size();i++) {
String a = alProductss.get(i).getAnount();
String b = alProductss.get(i).getProduct_id();
JSONObject productObject = new JSONObject();
jsonObject.accumulate("products", productObject);
JSONObject numberObject = new JSONObject();
numberObject.accumulate("product_id", b);
numberObject.accumulate("amount", a);
productObject.accumulate(b, numberObject);
}
I am getting this Responce:
{
"user_id":"230",
"shipping_id":"1",
"payment_id":"14",
"products":[
{"579":
{
"product_id":"579",
"amount":"1"
}
},
{"593":
{
"product_id":"593",
"amount":"1"
}
}]
}
But I want this json Responce please help for getting resultant responce.
{
"user_id": "3",
"shipping_id": "1",
"payment_id": "2",
"products": {
"500": {
"product_id": "500",
"amount": "1"
},
"501": {
"product_id": "501",
"amount":"1"
}
}
}
You can use the following code to generate your JSON,
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put("user_id", usr);
jsonObject.put("shipping_id", shipping_id);
jsonObject.put("payment_id", payment_id);
JSONObject productValueObject = new JSONObject();
for (int i = 0; i < alProductss.size(); i++) {
String a = alProductss.get(i).getAmount();
String b = alProductss.get(i).getProduct_id();
JSONObject projectObj = new JSONObject();
projectObj.put("product_id", b);
projectObj.put("amount", a);
productValueObject.put(b, projectObj);
}
jsonObject.put("products", productValueObject);
} catch (JSONException e) {
e.printStackTrace();
}
try this one..
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("user_id", usr);
jsonObject.accumulate("shipping_id", shipping_id);
jsonObject.accumulate("payment_id", payment_id);
JSONArray jsonArray = new JSONArray();
for( i=0;i<alProductss.size();i++) {
String a = alProductss.get(i).getAnount();
String b = alProductss.get(i).getProduct_id();
JSONObject productObject = new JSONObject();
JSONObject numberObject = new JSONObject();
numberObject.accumulate("product_id", b);
numberObject.accumulate("amount", a);
jsonArray.put(numberObject);
}
jsonObject.put("products",jsonArray);
{
"num_results": 1,
"objects": [
{
"birth_date": null,
"computers": [],
"id": 1,
"name": "Read a book"
}
],
"page": 1,
"total_pages": 1
}
You can use native code
JSONObject jsonObject= new JSONObject(response);
jsonObject.getString("num_results");
JSONArray jsonArray = jsonObject.getJSONArray("objects");
//create a model class (Name MyModel) with 3 field name birth_date,id, name
MyModel myModel = null;
//create a vector to hold data
Vector<MyModel> dataHolderVector = new Vector<MyModel>();
for (int i = 0; i < jsonArray.length(); i++) {
myModel = new MyModel();
final JSONObject catchObject = jsonArray.getJSONObject(i);
try{
dto.setBirthDate(catchObject, "birth_date"));
dto.setId(catchObject, "id"));
dto.setName(Utility.getJsonText(catchObject, "name"));
}catch (Exception e){
}
dataHolderVector.addElement(myModel);
myModel = null;
}
*** you dont provide computers[] element
I have string json like this :
{
"listResult": {
"items": [
{
"id": "629047db-66d9-4986-ba3f-c75554198138",
"thumbnail": "http://maya-wdv-01.r.worldssl.net/39aa32db-6f50-4da1-8fd5-a5b001135b98/629047db-66d9-4986-ba3f-c75554198138/8cb69c17-0fdb-454c-bfb5-a5b9001a9d59.jpg"
},
{
"id": "fa872dc8-d2b3-4815-92ef-d90e903bc3d8",
"thumbnail": "http://maya-wdv-01.r.worldssl.net/39aa32db-6f50-4da1-8fd5-a5b001135b98/fa872dc8-d2b3-4815-92ef-d90e903bc3d8/c510c24f-5bfd-4a64-8851-a5b90017a38d.jpg"
}
],
"totalItems": 34,
"pageSize": 5,
"pageNumber": 1,
"totalPages": 7,
"searchTerm": null
}
}
I Try Parsing with code :
try {
JSONObject json = new JSONObject(response);
String listResult = json.getString(Variabel.listResult);
JSONArray items_obj = json.getJSONArray(Variabel.items);
int jumlah_list_data = items_obj.length();
if (jumlah_list_data > 0) {
for (int i = 0; i < jumlah_list_data; i++) {
JSONObject obj = items_obj.getJSONObject(i);
String id = obj.getString(Variabel.id);
String thumbnail = obj.getString(Variabel.thumbnail);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
But I getting Error :
org.json.JSONException: No value for items
So how to solve it ? sorry for my English
Instead of:
JSONObject json = new JSONObject(response);
String listResult = json.getString(Variabel.listResult);
JSONArray items_obj = json.getJSONArray(Variabel.items);
you should have:
JSONObject json = new JSONObject(response);
JSONObject listResult = json.getJSONObject(Variabel.listResult);
JSONArray items_obj = listResult.getJSONArray(Variabel.items);
Accordingly to the json you posted, items is direct child of listResult, so you have to use the JSONObject with key listResult to retrieve it. Change
JSONArray items_obj = json.getJSONArray(Variabel.items);
with
JSONArray items_obj = listResult.getJSONArray(Variabel.items);
I have the following JSON response i want to get separate values like CHIL SEZ IT Park,Coimbatore,Tamil Nadu,India
JSON String is:
[
{
"value": "CHIL SEZ IT Park",
"offset": 0
},
{
"value": "Coimbatore",
"offset": 18
},
{
"value": "Tamil Nadu",
"offset": 30
},
{
"value": "India",
"offset": 42
}
]
Try this:
ArrayList<String> arrayList = new ArrayList<>();
try {
JSONArray jsonArray = new JSONArray(responseStr);
for(int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String value = jsonObject.getString("value");
arrayList.add(value);
}
} catch (JSONException e) {
e.printStackTrace();
}
Place this function and pass the jsonString. You will get the ArrayList from the JSON.
public ArrayList<String> getPlaceName(String jsonString){
ArrayList<String> places = new ArrayList<>();
try{
JSONArray array = new JSONArray(jsonString);
for(int x=0;x<array.length();x++){
places.add(array.getJSONObject(x).getString("value"));
}
}catch (JSONException e){}
return places;
}