i trying to loop through JSON in Java for Android.
my JSON- looks like this:
{
"departureList": [
{
"stopID": "80000589",
"x": "10.88553",
"y": "48.36553",
"mapName": "WGS84[dd.ddddd]",
"area": "4",
"platform": "5",
"platformName": "5",
"stopName": "Augsburg Hbf",
"nameWO": "Hauptbahnhof",
"pointType": "Gleis",
"countdown": "1",
"dateTime": {
"year": "2018",
"month": "8",
"day": "28",
"weekday": "3",
"hour": "9",
"minute": "54"
},
"servingLine": {
"key": "57120",
"code": "6",
"number": "RB 57120 Regionalbahn",
"symbol": "",
"motType": "0",
"mtSubcode": "0",
"realtime": "0",
"direction": "Donauwörth, Bahnhof",
"directionFrom": "München Hbf",
"trainType": "RB",
"trainName": "Regionalbahn",
"trainNum": "57120",
"name": "Regionalbahn",
"liErgRiProj": {
"line": "90910",
"project": "j18",
"direction": "R",
"supplement": " ",
"network": "ddb"
},
"destID": "2505500",
"stateless": "ddb:90910: :R:j18"
},
"operator": {
"code": "00",
"name": "DB AG",
"publicCode": ""
},
"prevStopSeq": {
"name": "",
"nameWO": "",
"place": "",
"nameWithPlace": "",
"omc": "-1",
"placeID": "-1",
"platformName": "",
"desc": "",
"ref": {
"id": "0",
"area": "",
"platform": "",
"attrs": [],
"coords": "",
"arrDelay": "0",
"arrValid": "0",
"depDelay": "0",
"depValid": "0"
}
},
"onwardStopSeq": {
"name": "",
"nameWO": "",
"place": "",
"nameWithPlace": "",
"omc": "-1",
"placeID": "-1",
"platformName": "",
"desc": "",
"ref": {
"id": "0",
"area": "",
"platform": "",
"attrs": [],
"coords": "",
"arrDelay": "0",
"arrValid": "0",
"depDelay": "0",
"depValid": "0"
}
}
},
{
"stopID": "80000589",
"x": "10.88566",
"y": "48.36558",
"mapName": "WGS84[dd.ddddd]",
"area": "4",
"platform": "4",
"platformName": "4",
"stopName": "Augsburg Hbf",
"nameWO": "Hauptbahnhof",
"pointType": "Gleis",
"countdown": "2",
"dateTime": {
"year": "2018",
"month": "8",
"day": "28",
"weekday": "3",
"hour": "9",
"minute": "55"
},
"servingLine": {
"key": "511",
"code": "6",
"number": "ICE 511 InterCityExpress",
"symbol": "",
"motType": "0",
"mtSubcode": "0",
"realtime": "0",
"direction": "München Hbf",
"directionFrom": "Köln Hbf",
"trainType": "ICE",
"trainName": "InterCityExpress",
"trainNum": "511",
"name": "InterCityExpress",
"liErgRiProj": {
"line": "98X42",
"project": "j18",
"direction": "H",
"supplement": "A",
"network": "ddb"
},
"destID": "80000689",
"stateless": "ddb:98X42:A:H:j18"
},
"operator": {
"code": "00",
"name": "DB AG",
"publicCode": ""
},
"attrs": [
{
"name": "lineType",
"value": "HIGHSPEEDTRAIN"
},
{
"name": "lineType",
"value": "LONG_DISTANCE_TRAINS"
},
{
"name": "lineType",
"value": "SUPPLEMENT"
}
],
"prevStopSeq": {
"name": "",
"nameWO": "",
"place": "",
"nameWithPlace": "",
"omc": "-1",
"placeID": "-1",
"platformName": "",
"desc": "",
"ref": {
"id": "0",
"area": "",
"platform": "",
"attrs": [],
"coords": "",
"arrDelay": "0",
"arrValid": "0",
"depDelay": "0",
"depValid": "0"
}
},
"onwardStopSeq": {
"name": "",
"nameWO": "",
"place": "",
"nameWithPlace": "",
"omc": "-1",
"placeID": "-1",
"platformName": "",
"desc": "",
"ref": {
"id": "0",
"area": "",
"platform": "",
"attrs": [],
"coords": "",
"arrDelay": "0",
"arrValid": "0",
"depDelay": "0",
"depValid": "0"
}
}
}
]
}
My goal is to get the values for the attribute number under the array servingLine.
JSONObject parentObject = new JSONObject(finalJson);
deparray = parentObject.getJSONArray("departureList");
JSONObject getdep= deparray.toJSONObject(deparray);
JSONArray getservingArray = getdep.getJSONArray("servingLine");
JSONObject lineobject= getservingArray.toJSONObject(getservingArray);
String trainnumber = lineobject.getString("number");
My Idea behind the code:
Convert the whole JSON-String in a JSON Object.
Look in this Object for the JSON-Array departureList
then look in this Array for servingLine, get it and convert it to an JSON-Object.
Now look for the specific string.
What is wrong on this idea ?
My problem is, that, i get the error "No value for servingLine". But why? And how can i fix it? It seems, he can't parse the whole JSON-String. And how can i prevent this?
JSONObject parentObject = new JSONObject(finalJson);
JSONArray deparray = parentObject.getJSONArray("departureList");
for (int i = 0; i < deparray.length(); i++) {
JSONObject getdep = deparray.getJSONObject(i);
JSONObject lineobject = getdep.getJSONObject("servingLine");
String trainnumber = lineobject.getString("number");
}
This is how I would do it assuming that given json is correct one.
Related
I have json array ob object list name of array is value in this array i have on inside value object i have another object called filed i want access filed object and find duplicate any help?.
Update: i used System.out.println("a : " + Collections.frequency(stringList, "Accounts Receivable")); method but its give me 0 always
{
"",
"value": [
{
"#odata.etag": """,
"createdDateTime": "",
"eTag": """,
"id": "",
"lastModifiedDateTime": "",
"webUrl": "",
"createdBy": {
"user": {
"email": "",
"id": "",
"displayName": ""
}
},
"lastModifiedBy": {
"user": {
"email": ",
"id": "",
"displayName": ""
}
},
"parentReference": {},
"contentType": {
"id": ""
},
"fields#odata.context": "",
"fields": {
"#odata.etag": """,
"id": "",
"ContentType": "",
"Modified": "",
"Created": "",
"AuthorLookupId": "",
"EditorLookupId": "",
"_UIVersionString": "",
"Attachments": false,
"Edit": "",
"ItemChildCount": "0",
"FolderChildCount": "0",
"_ComplianceFlags": "",
"_ComplianceTag": "",
"_ComplianceTagWrittenTime": "",
"_ComplianceTagUserId": "",
"JobCategory": "",--> "check this value for duplicate"
"Competency": "",
"Level": "",
"CompetencyType": "",
"Index": "0",
"FormID#odata.type": "",
"FormID": 25
}
}]
in side value array list there is fields object but while trying its given only object here is my Code.
Set<Example> uniqueSet = new HashSet<Example>(borrowModelList);
for (Example temp : uniqueSet) {
txtInfo.setText(String.valueOf(Collections.frequency(borrowModelList,temp.getValue().get(0).getFields())));
textInfoOne.setText(String.valueOf(Collections.frequency(borrowModelList,temp.getValue().get(0).getFields())));
textInfoTwo.setText(String.valueOf(Collections.frequency(borrowModelList,temp.getValue().get(0).getFields())));
textInfoThree.setText(String.valueOf(Collections.frequency(borrowModelList,temp.getValue().get(0).getFields())));
textInfoFour.setText(String.valueOf(Collections.frequency(borrowModelList,temp.getValue().get(0).getFields())));
}
I/System.out: 1
in side field object there is JobCategory field i want to check only and show on textview.
hi I am using okhttp for getting my data from the server and I am getting the response by response.body().string() that is printing full data. But when i am converting the response to jsonobject or String(Storing in the variable) and printing data it is not displaying full data of the response.i am unable to display
"id": 1,
"user_id": 1,
"first_name": "hhhh",
"last_name": "Thakur",
while other is working fine
My json data in form
{
"customers": [
{
"id": 1,
"user_id": 1,
"first_name": "hhhh",
"last_name": "Thakur",
"website": "",
"status": 0,
"created": "2017-10-10T07:29:45+00:00",
"customer_info": [
{
"id": 1,
"customer_id": 1,
"created": "2017-10-10T07:29:45+00:00"
}
],
"customer_address": [
{
"id": 1,
"customer_id": 1,
"name": "hhhh Thakur",
"address": "V.PO Chadwal Distt Kathua, Teh Hiranagar Jammu, Jammu and Kashmir in",
"city": "Shimla",
"state": "Himachal Pradesh",
"zip": "171004",
"country": "India",
"fax": "06723",
"type": 1,
"as_ship": 1,
"created": "2017-10-10T07:29:45+00:00"
},
{
"id": 2,
"customer_id": 1,
"name": "Neha Sharma",
"address": "V.PO Chadwal Distt Kathua, Teh Hiranagar Jammu, Jammu and Kashmir in",
"city": "India",
"state": "Jammu and Kashmir",
"zip": "180012",
"country": "India",
"fax": "664984",
"type": 2,
"as_ship": 1,
"created": "2017-10-10T07:29:45+00:00"
}
]
},
{
"id": 2,
"user_id": 1,
"first_name": "arun",
"last_name": "kumar",
"website": "www.isitwa.com",
"status": 0,
"created": "2017-10-11T10:14:38+00:00",
"customer_info": [
{
"id": 2,
"customer_id": 2,
"created": "2017-10-11T10:14:38+00:00"
}
],
"customer_address": [
{
"id": 3,
"customer_id": 2,
"name": "",
"address": "",
"city": "",
"state": "",
"zip": "",
"country": "",
"fax": "",
"type": 1,
"as_ship": 1,
"created": "2017-10-11T10:14:38+00:00"
},
{
"id": 4,
"customer_id": 2,
"name": "",
"address": "",
"city": "",
"state": "",
"zip": "",
"country": "",
"fax": "",
"type": 2,
"as_ship": 1,
"created": "2017-10-11T10:14:38+00:00"
}
]
}
]}
}
That's because your JSON is invalid... Paste your JSON codes here, https://codebeautify.org/jsonviewer , it will allow you to manipulate the formating, and take a better look into it...
I'm not an expert in Java but listing a nested array ( no matter, JSON or plain array ) can be done in two ways:
foreach ( $array as $item) {
foreach ( $item as $key => $data) {
echo $data;
}
}
for ( $i=0; $i < $array.lenght(); $i++ ) {
for ( $j=0; $j < $array.lenght-1; $j++) {
print_r $array[$i][$j];
}
}
Using foreach method is better if you don't know how long, or nested your array is...
Your json data is not well formatted ,try to print below one:
{
"a": [
{
"id": 1,
"user_id": 1,
"first_name": "John",
"last_name": "",
"b": [
{
"id": 1,
"customer_id": 1,
"created": "2017-10-10T07:29:45+00:00"
}
],
"c": [
{
"id": 1,
"customer_id": 1,
"created": "2017-10-10T07:29:45+00:00"
}
]
}],
"b":[{
"id": 1,
"user_id": 1,
"first_name": "John",
"last_name": "",
"b": [
{
"id": 1,
"customer_id": 1,
"created": "2017-10-10T07:29:45+00:00"
}
],
"c": [
{
"id": 1,
"customer_id": 1,
"created": "2017-10-10T07:29:45+00:00"
}
]
}
]}
Iam new to jolt.
Can you please tell me how can i trasform the below json message with array based upon the position in to the below output json message using jolt.
input message :
[
["20084541", "12020584", "Frohmann Dov", "2017", "2", "75", "T7", "DFZ", "CES", "", "", "0", "90", "2010"],
["20084541", "12020584", "Frohmann Dov", "2017", "3", "21", "T7", "DFZ", "CES", "", "", "0", "90", "2010"],
]
output Message :
[{
"policyReference": "20084541",
"insuredId": "12020584",
"insuredName": "Frohmann Dov",
"uwy": "2017",
"subLOB": "2",
"typeOfRisk": "75",
"aircraftcountryCode": "T7",
"aircraftId": "DFZ",
"manufacturerId": "CES",
"aircraftTypeCode": "",
"aircraftSubTypeCode": "",
"aircraftValueAmt": "0",
"aircraftWorkNo": "90",
"yearBuilt": "2010"
}, {
"policyReference": "20084541",
"insuredId": "12020584",
"insuredName": "Frohmann Dov",
"uwy": "2017",
"subLOB": "2",
"typeOfRisk": "75",
"aircraftcountryCode": "T7",
"aircraftId": "DFZ",
"manufacturerId": "CES",
"aircraftTypeCode": "",
"aircraftSubTypeCode": "",
"aircraftValueAmt": "0",
"aircraftWorkNo": "90",
"yearBuilt": "2010"
}]
Any help appreciated.
Spec
[
{
"operation": "shift",
"spec": {
"*": { // loop thru the outer array
// each item of the outer array, is an array
// match individual array indicies, and then send them to the
// output with the nice name.
"0": "[&1].policyReference",
"1": "[&1].insuredId",
"2": "[&1].insuredName"
// etc
}
}
}
]
I have this code to upload data to database
RequestParams params = new RequestParams();
//prgDialog.show();
params.put("usersJSON", controller.getAllAnswerSheet());
System.out.println("onSuccess getAllAnswerSheet" + controller.numberOfRows("answer_sheet_tbl"));
System.out.println("onSuccess getAllAnswerSheet" + controller.getAllAnswerSheet());
// Make Http call to updatesyncsts.php with JSON parameter which has Sync statuses of Users
client.post("http://127.0.0.1/qna/process/uploadanswersheet.php", params, new AsyncHttpResponseHandler() {
I know i have data because i get this in log
[{
"answer": [{
"sysid": "1",
"surveyid": "1",
"questionid": "0",
"answerid": "",
"householdid": "1",
"status": "active"
}, {
"sysid": "2",
"surveyid": "1",
"questionid": "0",
"answerid": "",
"householdid": "2",
"status": "active"
}, {
"sysid": "3",
"surveyid": "1",
"questionid": "0",
"answerid": "",
"householdid": "3",
"status": "active"
}, {
"sysid": "4",
"surveyid": "1",
"questionid": "0",
"answerid": "",
"householdid": "4",
"status": "active"
}, {
"sysid": "5",
"surveyid": "1",
"questionid": "0",
"answerid": "",
"householdid": "5",
"status": "active"
}, {
"sysid": "6",
"surveyid": "1",
"questionid": "0",
"answerid": "",
"householdid": "6",
"status": "active"
}, {
"sysid": "7",
"surveyid": "1",
"questionid": "0",
"answerid": "",
"householdid": "7",
"status": "active"
}, {
"sysid": "8",
"surveyid": "1",
"questionid": "1",
"answerid": "aklan",
"householdid": "8",
"status": "active"
}, {
"sysid": "9",
"surveyid": "1",
"questionid": "2",
"answerid": "kalibo",
"householdid": "9",
"status": "active"
}, {
"sysid": "10",
"surveyid": "1",
"questionid": "3",
"answerid": "7",
"householdid": "10",
"status": "active"
}, {
"sysid": "11",
"surveyid": "1",
"questionid": "3",
"answerid": "7",
"householdid": "11",
"status": "active"
}, {
"sysid": "12",
"surveyid": "1",
"questionid": "3",
"answerid": "7",
"householdid": "12",
"status": "active"
}, {
"sysid": "13",
"surveyid": "1",
"questionid": "6",
"answerid": "three",
"householdid": "13",
"status": "active"
}, {
"sysid": "14",
"surveyid": "1",
"questionid": "6",
"answerid": "three",
"householdid": "14",
"status": "active"
}, {
"sysid": "15",
"surveyid": "1",
"questionid": "7",
"answerid": "patwo",
"householdid": "15",
"status": "active"
}, {
"sysid": "16",
"surveyid": "1",
"questionid": "7",
"answerid": "pathree",
"householdid": "16",
"status": "active"
}, {
"sysid": "17",
"surveyid": "1",
"questionid": "7",
"answerid": "pathree",
"householdid": "17",
"status": "active"
}],
"survey": [{
"sysid": "17",
"householdnum": "74",
"barangay": "manila",
"headfname": "martin",
"headmname": "",
"headlname": "manalo",
"headsuffix": "",
"status": "active"
}]
}]
My problem is in my uploadanswersheet.php page I get Undefined index: usersJSON Meaning I was not able to send the the data in params.
This is my code in uploadanswersheet.php:
//Get JSON posted by Android Application
$json = $_POST["usersJSON"];
//Remove Slashes
$json = json_decode(stripslashes($json), true);
//if (get_magic_quotes_gpc()){
//$json = stripslashes($json);
//}
Convert JSON to string and post it. And at server side convert back string to JSON
I can't understand, why I have this error:
04-24 22:11:51.263: W/System.err(27504): org.json.JSONException: Value
<!--HERE JSON VALUE--> at data of type org.json.JSONObject cannot be
converted to JSONArray
This is my code:
JSONObject getProgile = null;
try {
//get json
getProgile = new JSONObject(CustomHttpClient.executeHttpGet(profileGetURL).toString());
//convert array
JSONArray array = getProgile.getJSONArray("data");
for (int i = 0; i < array.length(); i++) {
JSONObject c = array.getJSONObject(i);
//get TAG_CUSTOMER
JSONObject customer = c.getJSONObject("Customer");
pName = customer.getString("name");
pLname = customer.getString("name");
}
UPD:
My json
{
"status": "success",
"data": {
"Customer": {
"id": "33",
"company_id": "1",
"name": "SDfsdf",
"birthdate": "14.02.1989",
"email": "dsfsdf#sf.ff",
"photo": "/files/clients_photos/33/(null)",
"bonuses": "50",
"created": "2015-02-14 12:22:46",
"modified": "2015-02-14 12:22:46",
"ref_id": null,
"ref_code": "6363696029",
"banned": null,
"ban_reason": null,
"ban_ending": null
},
"CustomerVisit": [],
"CustomerBonus": [
{
"id": "29",
"customer_id": "33",
"user_id": "4",
"product_id": null,
"operation": "plus",
"amount": "50",
"subject": "Загрузка фото при регистрации.",
"remain": null,
"modified": "2015-02-14 12:22:46",
"date": "14.02.2015",
"created": "14.02.2015 12:22"
}
],
"CustomerCar": [
{
"id": "41",
"customer_id": "33",
"car_brand_id": "9",
"car_model_id": "11530",
"year": "2020",
"vin": "sdfsdfsdf",
"photo": "",
"number": "dsfsdf",
"created": "2015-02-14 12:22:46",
"modified": "2015-02-14 12:22:46",
"car_brand_name": "BMW",
"car_model_name": "323"
}
],
"CustomerPhone": [
{
"id": "41",
"customer_id": "33",
"phone": "+380990010222",
"created": "2015-02-14 12:22:46",
"modified": "2015-02-14 12:22:46"
}
],
"Insurance": [],
"Event": [],
"Review": [],
"Reservation": []
}
}
JSON shows data is JSONObject rather than JSONArray, so call getProfile.getJSONObject ("data")
The problem is that you are trying to convert and JSON Object with JSON array therefore an error will occur.
If you just want to get the name then you can do this:
getProgile = new JSONObject(CustomHttpClient.executeHttpGet(profileGetURL).toString());
JSONObject obj1 = getProgile.getJSONObject("data");
JSONObject obj2 = array.getJSONObject("Customer");
String name = obj2.getString("name");