The following JSON string comes to me after a request. Whatever I try, I can not parse the data.
[{
"_id": {
"$oid": "5909846cbd966f2d371bc624"
},
"member_id": "NTkwOTEyNzdiZDk2NmYyZDM3MTkyY2M1",
"sensor_name": "Temprature",
"value": "27.28",
"date": "2017-05-03 10:19:07"
}]
Try using following code :
try {
JSONArray jsonArray = new JSONArray(response);
JSONObject jsonObject = jsonArray.get(0);
JSONObject _id = jsonObject.getJSONObject("_id");
String old = _id.getString("$oid");
String member_id = jsonObject.getString("member_id");
String sensor_name = jsonObject.getString("sensor_name");
String value = jsonObject.getString("value");
String date = jsonObject.getString("date");
}catch(JSONException e){
}
Related
-- I know parsing, i have successfully parsed below data, but when web-service is upgraded i need to parse object inside object.
- I tried many example but getting error.
Code for Parsing :
private final String KEY_SUCCESS = "status";
private final String KEY_MSG = "Message";
private final String KEY_MSG1 = "Message";
// private final String KEY_AddressList = "addressList";
private final String KEY_USERINFO = "user_info";
ArrayList<HashMap<String, String>> arraylist;
private final String KEY_DATA = "data";
private final String KEY_USERDATA = "userdata";
public ArrayList<HashMap<String, String>> getList(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
if (jsonObject.getString(KEY_SUCCESS).equals("true")) {
arraylist = new ArrayList<HashMap<String, String>>();
JSONArray jsonArray = jsonObject.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject obj = jsonArray.getJSONObject(i);
// JSONObject job = obj.getJSONObject("User");
Log.d("obj", obj.toString());
String user_id = obj.getString(Constants.Params.ID);
String createdate = obj.getStringAndyConstants.Params.CREATEDDATE);
String postImage = obj.getString(Constants.Params.IMAGE);
map.put(AndyConstants.Params.ID, user_id);
map.put(AndyConstants.Params.CREATEDDATE, createdate);
map.put(AndyConstants.Params.IMAGE, postImage);
JSONObject objectDetails2 = obj.getJSONObject(KEY_DATA);
JSONArray jsonArrayUser = objectDetails2.getJSONArray("userdata");
for (int j = 0; j < jsonArrayUser.length(); j++) {
HashMap<String, String> mapUser = new HashMap<String, String>();
JSONObject businessObject = jsonArrayUser.getJSONObject(j);
Log.d("obj", businessObject.toString());
}
Log.d("map", map.toString());
arraylist.add(map);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return arraylist;
}
Error Log :
org.json.JSONException: No value for data
E/array: []
E/adapter: []
Json :
{
"status": true,
"message": "Successfully add Post",
"data": [{
"user_id": "46",
"image": "",
"createdate": "2016-05-11 06:05:12",
"userdata": {
"first_name": "Alpha",
"last_name": "Gamma",
"image": "http:\/\/abcd.net\/abcd\/uploads\/100520161005191462941615032.jpg"
}
}
You dont need this line
JSONObject objectDetails2 = obj.getJSONObject(KEY_DATA);
and change
JSONArray jsonArrayUser = objectDetails2.getJSONArray("userdata");
to
JSONObject userData = obj.getJSONObject("userdata");
String firstName = userData.getString("first_name");
String lastName = userData.getString("last_name");
As you can see from the JSON
"data":
[{"user_id":"46",
"image":"",
"createdate":"2016-05-11 06:05:12",
"userdata":{"first_name
data is having the object [ which denotes an Array while userData shows a { which is a JSONObject
I notice that your JSON has some errors. is this the complete Json response?
Because JSON response should be something like this:
{
"status": true,
"message": "Successfully add Post",
"data": [{
"user_id": "46",
"image": "",
"createdate": "2016-05-11 06:05:12",
"userdata": {
"first_name": "Alpha",
"last_name": "Gamma",
"image": "http:\/\/abcd.net\/abcd\/uploads\/100520161005191462941615032.jpg"
}
}]
}
You will notice that there is no close for [ after userdata in your Json.
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 got an jsonarray:
[
{
"ean": "8020079127",
"nr": "100",
"name": "Name1"
},
{
"ean": "8026180222",
"nr": "4",
"name": "Name2"
},
{
"ean": "6577426092",
"nr": "1",
"name": "Name3"
}
]
I need the value from "nr" depending on the "ean" means:
I got the ean 8026180222 (as string) and need the value from the "nr" (here "4"). how I can get it?
its possible without iterate over the whole array?
thank you
Try this:
// Use a map to store ean,nr (as key/value)
HashMap<String, String> jsonMap = new HashMap<String, String>();
// Load the data from json
JSONArray jsonArray= new JSONArray(yourJsonString);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObj = jsonArray.getJSONObject(i);
String ean = jsonObj.getString("ean");
String nr = jsonObj.getString("nr");
String name = jsonObj.getString("name");
jsonMap.put(ean, nr); // here you put ean as key and nr as value
}
To retrieve an nr value of a specific ean just call: jsonMap.get(yourEAN);
Example: String nr = jsonMap.get("8026180222");
List<NameValuePair> params = new ArrayList<NameValuePair>();
JsonObject json = jsonParser.makeHttpRequest(url, "GET", params);
Log.d("Tag", json.toString());
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
JSONArray jsnObj;
try {
jsnObj = json
.getJSONArray(TAG_ARRAY);
JSONObject obj = jsnObj
.getJSONObject(1);
String nr = obj.getString(TAG_NR);
}
Catch(Exception ex){}
}
}
If you want array you can use loops in json array.....................Hope it will help
Just define
private static final String TAG_SUCCESS = "success";
private static final String TAG_NR = "nr";
private static final String TAG_ARRAY = "array_name";
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
i want to get the values from the below nested json string and pass it to listview, i'm confused about it. could somebody help it out from this problem.Thanks in advance
JSON DATA :
{
"errstr": "Apps list",
"success": 1,
"data": {
"topFree": [
{
"AppID": "3",
"Title": "rgrger",
"Price": null,
"Rating": ""
},
{
"AppID": "10",
"Title": "dwdqw",
"Price": null,
"Rating": ""
}
],
"topPaid": [
{
"AppID": "14",
"Title": "erfwerwe",
"Price": "0.00",
"Rating": ""
}
],
"magazine": [
{
"AppID": "65",
"Title": "wdfwfqw",
"Price": "0.00",
"Rating": ""
}
],
"books": "",
"onlineApp": ""
}
}
Try this..
try {
JSONObject result = new JSONObject(response);
JSONObject data = result.getJSONObject("data");
// check for your conditions... i am directly calling array now..
if(data.has("topFree"){
JSONArray array = data.getJSONArray("topFree");
//creating array for storing values... you caN create with your class..
arr_list = new ArrayList<String>();
for (int i = 0; i < array.length(); i++) {
JSONObject obj = array.getJSONObject(i);
String AppID = ""+obj.getString("AppID");
String Title = ""+obj.getString("Title");
String Price = ""+obj.getString("Price");
String Rating = ""+obj.getString("Rating");
arr_list.add(AppID); // adding values to array.
}
}
// do the same for other arrays
} catch (JSONException e) {
e.printStackTrace();
}
After you get data.. you can add values to listview adapter..
Step 1: Parse JSONObject first, assuming that you have got this JSON as a response and you are unable to parse it.
JSONObject jsonObj = new JSONObject(mJsonString);
// mJsonString is the json you have received as a response
String errstr = jsonObj.getString("errstr");
String success = jsonObj.getString("success");
String data = jsonObj.getString("data");
JSONObject jsonObjData = new JSONObject(data);
String topFree = jsonObjData.getString("topFree");
if(topFree != null){
JSONArray mArray = new JSONArray(topFree);
for(int i=0;i<mArray.length;i++){
JSONObject mJsonObj = new JSONObject(
mArray.getString(i));
String appId = mJsonObj.getString("AppID");
String title = mJsonObj.getString("Title");
String price = mJsonObj.getString("Price");
String rating = mJsonObj.getString("Rating");
}
String topPaid = jsonObjData.getString("topPaid");
if(topPaid != null){
JSONArray mArrayTopPaid = new JSONArray(topPaid);
for(int i=0;i<mArrayTopPaid.length;i++){
JSONObject mJsonObj = new JSONObject(
mArrayTopPaid.getString(i));
String appId = mJsonObj.getString("AppID");
String title = mJsonObj.getString("Title");
String price = mJsonObj.getString("Price");
String rating = mJsonObj.getString("Rating");
}
String magazine = jsonObjData.getString("magazine");
if(magazine != null){
JSONArray mArrayMagazine = new JSONArray(magazine);
for(int i=0;i<mArrayMagazine.length;i++){
JSONObject mJsonObj = new JSONObject(
mArrayMagazine.getString(i));
String appId = mJsonObj.getString("AppID");
String title = mJsonObj.getString("Title");
String price = mJsonObj.getString("Price");
String rating = mJsonObj.getString("Rating");
}
String books = jsonObjData.getString("books");
String onlineApp = jsonObjData.getString("onlineApp");
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Guys can anyone help me to parse the below JSON data:
[
{
"oneMethod": {
"NewData": {
"Table": {
"Column1": "1",
"Column2": "2",
"Column3": "3",
"Column4": "4",
"Column5": "5",
"Column6": "6"
}
}
}
}
]
I am using this:
JSONObject reader = new JSONObject(jsonStr);
JSONObject sys = reader.getJSONObject("Table");
id = sys.getString("Column1");
id2 = sys.getString("Column1");
id3 = sys.getString("Column1");
id4 = sys.getString("Column1");
id5 = sys.getString("Column1");
id6 = sys.getString("Column1");
But it's saying
JSONArray cannot be converted to JSONObject
Try this..
[ //JSONArray Jarray
{ //JSONObject Jobject
"someMethod": { //JSONObject object_someMethod
"NewDataSet": { //JSONObject object_NewDataSet
"Table": { //JSONObject object_Table
"Column1": "1",
"Column2": "2",
"Column3": "3",
"Column4": "4",
"Column5": "5",
"Column6": "6"
}
}
}
}
]
Code:
JSONArray Jarray = new JSONArray(result);
JSONObject Jobject = Jarray.getJSONObject(0);
JSONObject object_someMethod = Jobject.getJSONObject("someMethod");
JSONObject object_NewDataSet = object_someMethod.getJSONObject("NewDataSet");
JSONObject object_Table = object_NewDataSet.getJSONObject("Table");
Log.v("Column1",""+object_Table.getString("Column1"));
Log.v("Column2",""+object_Table.getString("Column2"));
Log.v("Column3",""+object_Table.getString("Column3"));
Log.v("Column4",""+object_Table.getString("Column4"));
Log.v("Column5",""+object_Table.getString("Column5"));
Log.v("Column6",""+object_Table.getString("Column6"));
[ // json array node
{ // json object node
"someMethod": { // json object somemethod
"NewDataSet": { //json object NewDataSet
"Table": { //json object Table
"Column1": "1",
"Column2": "2",
"Column3": "3",
"Column4": "4",
"Column5": "5",
"Column6": "6"
}
}
}
}
]
Look at the docs
http://developer.android.com/reference/org/json/JSONArray.html
And
http://developer.android.com/reference/org/json/JSONObject.html
To parse
JSONArray jr = new JSONArray("your json string");
// at the top you have a json array node
JSONObject jb = (JSONObject)jr.get(0);
// then you have a json object. Use the index and then cast to JSONObject
JSONObject jb1 = (JSONObject) jb.getJSONObject("someMethod");
JSONObject jb2 = (JSONObject)jb1.getJSONObject("NewDataSet");
JSONObject jb3 = (JSONObject)jb2.getJSONObject("Table");
String col1 = jb3.getString("Column1");
String col2 = jb3.getString("Column2");
String col3 = jb3.getString("Column3");
String col4 = jb3.getString("Column4");
String col5 = jb3.getString("Column5");
String col6 = jb3.getString("Column6");
Problem is at JSONObject reader = new JSONObject(jsonStr); as you are getting array while you are using object which is causing a problem!!
always remember
[ means array
{ means object
so have a look on following code
JSONArray ja = new JSONArray(jsonStr);
JSONObject jo = (JSONObject)ja.get(0);
JSONObject jo1 = jo.getJSONObject("someMethod");
JSONObject jo2 = jo1.getJSONObject("NewDataSet");
JSONObject jo3 = jo2.getJSONObject("Table");
String column1 = jo3.getString("Column1");
String column2 = jo3.getString("Column2");
String column3 = jo3.getString("Column3");
String column4 = jo3.getString("Column4");
String column5 = jo3.getString("Column5");
String column6 = jo3.getString("Column6");