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
Related
I am building a Java Android App with Webserver (Raspberry Pi 4 with LAMP) connection for the first time.
There's a db table "products" which holds all products (Product_id, productName, price). I am retrieving a JSON array though a PHP API.
On App launch, I want to get all products and create Java Objects out of them, so I can dynamically display all products and prices in a Fragment in my app. Note that number and price of products might change, so I need this approach to dynamically create the products.
Since I have never done this before, I am stuck. I know how to get a response in my code, but what I need help with is to turn that response into Java Objects. Any advice how you usually would do that?
Here's my JSON:
"status": 200,
"info": [
{
"Product_id": "1",
"product": "Coca Cola",
"price": "3"
},
{
"Product_id": "2",
"product": "Orange Juice",
"price": "2.5"
},
{
"Product_id": "3",
"product": "Apple Juice",
"price": "2.5"
},
{
"Product_id": "4",
"product": "Beer",
"price": "1.5"
},
{
"Product_id": "5",
"product": "Energy Drink",
"price": "3"
},
{
"Product_id": "6",
"product": "Gin Tonic",
"price": "5"
},
{
"Product_id": "7",
"product": "Water",
"price": "1"
},
{
"Product_id": "8",
"product": "Soda",
"price": "1"
},
{
"Product_id": "9",
"product": "Ticket",
"price": "30"
}
]
}
I just used Gson, as Manuel and Ibrahim suggested. Thank you!
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.
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 some data select from mysql. Then I want to format it.
The data just like:
var json = [
{
"date": "2016-01",
"number": "1",
"code": "420000",
"type": "false"
},
{
"date": "2016-01",
"number": "2",
"code": "440000",
"type": "false"
},
{
"date": "2016-02",
"number": "3",
"code": "420000",
"type": "false"
},
{
"date": "2016-03",
"number": "4",
"code": "420000",
"type": "true"
},
{
"date": "2016-03",
"number": "5",
"code": "410000",
"type": "false"
},
{
"date": "2016-03",
"number": "6",
"code": "440000",
"type": "true"
},
{
"date": "2016-04",
"number": "7",
"code": "420000",
"type": "false"
},
{
"date": "2016-04",
"number": "8",
"code": "440000",
"type": "false"
}
];
console.log(json);
I want to groupingBy date, and partitioningBy type, and reducing to a Map.
after format data just like
var json = {
"2016-01": {
"false": {
"420000": "1",
"440000": "2"
}
},
"2016-02": {
"false": {
"420000": "3"
}
},
"2016-03": {
"false": {
"410000": "5"
},
"true": {
"420000": "4",
"440000": "6"
}
},
"2016-04": {
"false": {
"420000": "7",
"440000": "8"
}
}
};
console.log(json);
I coding java8 Stream
public static void main(String[] args) {
List<Map<String, String>> postmans = new ArrayList<>();
// "420000" : post area code, "false": just paper(without goods)
postmans.add(createMap("2016-01", "1", "420000", "false"));
postmans.add(createMap("2016-01", "2", "440000", "false"));
postmans.add(createMap("2016-02", "3", "420000", "false"));
postmans.add(createMap("2016-03", "4", "420000", "true"));
postmans.add(createMap("2016-03", "5", "410000", "false"));
postmans.add(createMap("2016-03", "6", "440000", "true"));
postmans.add(createMap("2016-04", "7", "420000", "false"));
postmans.add(createMap("2016-04", "8", "440000", "false"));
// before, I use fastjson Library
System.out.println(JSONObject.toJSONString(postmans));
Map<String, Map<Boolean, Map>> data = postmans.stream()
.collect(Collectors.groupingBy(d -> d.get("date"), TreeMap::new,
Collectors.partitioningBy(d-> d.get("type").equals("true"),
Collectors.reducing(new HashMap(), (d1, d2)->{
Object code = d2.get("code");
Object number = d2.get("number");
// I think bug in reducing
d1.put(code, number);
return d1;
}))));
// after, I use fastjson Library
System.out.println(JSONObject.toJSONString(data));
}
private static Map<String,String> createMap(String date, String number, String code, String type){
Map<String, String> map = new HashMap<>();
map.put("date", date);
map.put("number", number);
map.put("code", code);
map.put("type", type);
return map;
}
but, real result is
var json = {
"2016-01": {
"false": {
"410000": "5",
"420000": "7",
"440000": "8"
},
"true": {
"410000": "5",
"420000": "7",
"440000": "8"
}
},
"2016-02": {
"false": {
"410000": "5",
"420000": "7",
"440000": "8"
},
"true": {
"410000": "5",
"420000": "7",
"440000": "8"
}
},
"2016-03": {
"false": {
"410000": "5",
"420000": "7",
"440000": "8"
},
"true": {
"410000": "5",
"420000": "7",
"440000": "8"
}
},
"2016-04": {
"false": {
"410000": "5",
"420000": "7",
"440000": "8"
},
"true": {
"410000": "5",
"420000": "7",
"440000": "8"
}
}
};
console.log(json);
I think the cause of this bug in reduing. When the partitioningBy get a new List <Map>, the old d1 still exists.
I just want to partitioningBy result, each List <Map> compressed to a Map, I can guarantee that its key (code) is unique.
I am sorry for The code is so long and My English is poor. Thank you for help me!
There are two main mistakes here. Collectors.reducing, since this is a reduction, needs to return a new instance all the time and basically the second is that your logic is flawed:
Map<String, Map<Boolean, Map<String, String>>> data = postmans.stream()
.collect(Collectors.groupingBy(
d -> d.get("date"),
TreeMap::new,
Collectors.partitioningBy(
d -> d.get("type").equals("true"),
Collectors.reducing(
new HashMap<>(),
(left, right) -> {
Map<String, String> map = new HashMap<>();
String leftCode = left.get("code");
String leftNumber = left.get("number");
if (leftCode == null) {
map.putAll(left);
} else {
map.put(leftCode, leftNumber);
}
String rightCode = right.get("code");
String rightNumber = right.get("number");
map.put(rightCode, rightNumber);
return map;
}))));
You also have a problem of storing Date as String and doing TreeMap::new - which basically sorts Stringss, which is probably not the sorting you want. In general this entire approach of having everything as a String makes the code hardly readable.
I am having this in JSONArray ary2;
JSON string is :
[
{
"item_id": "1",
"Head_item_id": "1",
"major_item_id": "1",
"Quantity": "10",
"selling_prize": "20",
"MRP": "90",
"title": "JK Lakshmi PPC Cement",
"SKU": "B2WBUICEM2"
},
{
"item_id": "2",
"Head_item_id": "1",
"major_item_id": "1",
"Quantity": "10",
"selling_prize": "30",
"MRP": "80",
"title": "JK Lakshmi PPC Cement",
"SKU": "B2WBUICEM2"
},
{
"item_id": "3",
"Head_item_id": "1",
"major_item_id": "1",
"Quantity": "10",
"selling_prize": "10",
"MRP": "70",
"title": "Shree Ultra OPC 43 Grade Cement",
"SKU": "B2WBUICEM5"
}
]
I want to get String array for each attribute , like :
String[] item_id;
String[] Head_item_id;
etc.
unable to get it. Please help.
Try This:
JSONArray arr = new JSONArray(yourJSONresponse);
String[] item_id=new String[array.length()];
String[] Head_item_id=new String[arr .length()];
for(int i = 0; i < arr.length(); i++){
item_id[i]=arr.getJSONObject(i).getString("item_id");
Head_item_id[i]=arr.getJSONObject(i).getString("Head_item_id");
}
You need to follow these steps:
-> Create POJO for your JSON data and provite getters and setters for each attribute.
-> Parse your JSON data using GSON
-> get the desired attribute(using getters) and store it in the respective string array