Problem with Jackson ArrayNode.add Java function - java

I have a function that should change some values on Object that have some objects of array.
This Object is:
{"avg":{"00:00":[],"01:00":[],"02:00":[],"03:00":[],"04:00":[],"05:00":[],"06:00":[],"07:00":[],"08:00":[],"09:00":[],"10:00":[],"11:00":[],"12:00":[],"13:00":[],"14:00":[],"15:00":[],"16:00":[],"17:00":[],"18:00":[],"19:00":[],"20:00":[],"21:00":[],"22:00":[],"23:00":[]},"avg_hum":{"00:00":[],"01:00":[],"02:00":[],"03:00":[],"04:00":[],"05:00":[],"06:00":[],"07:00":[],"08:00":[],"09:00":[],"10:00":[],"11:00":[],"12:00":[],"13:00":[],"14:00":[],"15:00":[],"16:00":[],"17:00":[],"18:00":[],"19:00":[],"20:00":[],"21:00":[],"22:00":[],"23:00":[]},"max":{"00:00":[],"01:00":[],"02:00":[],"03:00":[],"04:00":[],"05:00":[],"06:00":[],"07:00":[],"08:00":[],"09:00":[],"10:00":[],"11:00":[],"12:00":[],"13:00":[],"14:00":[],"15:00":[],"16:00":[],"17:00":[],"18:00":[],"19:00":[],"20:00":[],"21:00":[],"22:00":[],"23:00":[]},"min":{"00:00":[],"01:00":[],"02:00":[],"03:00":[],"04:00":[],"05:00":[],"06:00":[],"07:00":[],"08:00":[],"09:00":[],"10:00":[],"11:00":[],"12:00":[],"13:00":[],"14:00":[],"15:00":[],"16:00":[],"17:00":[],"18:00":[],"19:00":[],"20:00":[],"21:00":[],"22:00":[],"23:00":[]},"max_hum":{"00:00":[],"01:00":[],"02:00":[],"03:00":[],"04:00":[],"05:00":[],"06:00":[],"07:00":[],"08:00":[],"09:00":[],"10:00":[],"11:00":[],"12:00":[],"13:00":[],"14:00":[],"15:00":[],"16:00":[],"17:00":[],"18:00":[],"19:00":[],"20:00":[],"21:00":[],"22:00":[],"23:00":[]},"min_hum":{"00:00":[],"01:00":[],"02:00":[],"03:00":[],"04:00":[],"05:00":[],"06:00":[],"07:00":[],"08:00":[],"09:00":[],"10:00":[],"11:00":[],"12:00":[],"13:00":[],"14:00":[],"15:00":[],"16:00":[],"17:00":[],"18:00":[],"19:00":[],"20:00":[],"21:00":[],"22:00":[],"23:00":[]},"beginDate":"","endDate":""}
And I need to add values to arrays (like: {"00:00": [25],...})
So, I tried something like ("data_raw" is the received object):
ArrayNode array = mapper.createArrayNode();
array = (ArrayNode) data_raw.get("avg").get("00:00");
array.add((double) 25.11666711171468);
System.out.println("data_raw: " + data_raw.toString());
And the result ("data_raw" object) is:
data_raw: {"avg":{"00:00":[25.11666711171468],"01:00":[],"02:00":[],"03:00":[],"04:00":[],"05:00":[],"06:00":[],"07:00":[],"08:00":[],"09:00":[],"10:00":[],"11:00":[],"12:00":[],"13:00":[],"14:00":[],"15:00":[],"16:00":[],"17:00":[],"18:00":[],"19:00":[],"20:00":[],"21:00":[],"22:00":[],"23:00":[]},"avg_hum":{"00:00":[25.11666711171468],"01:00":[],"02:00":[],"03:00":[],"04:00":[],"05:00":[],"06:00":[],"07:00":[],"08:00":[],"09:00":[],"10:00":[],"11:00":[],"12:00":[],"13:00":[],"14:00":[],"15:00":[],"16:00":[],"17:00":[],"18:00":[],"19:00":[],"20:00":[],"21:00":[],"22:00":[],"23:00":[]},"max":{"00:00":[25.11666711171468],"01:00":[],"02:00":[],"03:00":[],"04:00":[],"05:00":[],"06:00":[],"07:00":[],"08:00":[],"09:00":[],"10:00":[],"11:00":[],"12:00":[],"13:00":[],"14:00":[],"15:00":[],"16:00":[],"17:00":[],"18:00":[],"19:00":[],"20:00":[],"21:00":[],"22:00":[],"23:00":[]},"min":{"00:00":[25.11666711171468],"01:00":[],"02:00":[],"03:00":[],"04:00":[],"05:00":[],"06:00":[],"07:00":[],"08:00":[],"09:00":[],"10:00":[],"11:00":[],"12:00":[],"13:00":[],"14:00":[],"15:00":[],"16:00":[],"17:00":[],"18:00":[],"19:00":[],"20:00":[],"21:00":[],"22:00":[],"23:00":[]},"max_hum":{"00:00":[25.11666711171468],"01:00":[],"02:00":[],"03:00":[],"04:00":[],"05:00":[],"06:00":[],"07:00":[],"08:00":[],"09:00":[],"10:00":[],"11:00":[],"12:00":[],"13:00":[],"14:00":[],"15:00":[],"16:00":[],"17:00":[],"18:00":[],"19:00":[],"20:00":[],"21:00":[],"22:00":[],"23:00":[]},"min_hum":{"00:00":[25.11666711171468],"01:00":[],"02:00":[],"03:00":[],"04:00":[],"05:00":[],"06:00":[],"07:00":[],"08:00":[],"09:00":[],"10:00":[],"11:00":[],"12:00":[],"13:00":[],"14:00":[],"15:00":[],"16:00":[],"17:00":[],"18:00":[],"19:00":[],"20:00":[],"21:00":[],"22:00":[],"23:00":[]},"beginDate":"","endDate":""}
I expect that just "avg":{"00:00":[X],...} be changed, but all objects with "00:00" name are being replaced regardless of the selected "primary field": "avg".
Can someone help me?
EDIT:
Complete related code:
String[] name_db_data = {"avg", "avg_hum", "max", "min", "max_hum", "min_hum"};
SimpleDateFormat hourFormatted = new SimpleDateFormat("HH:mm");
reType = reportType.detailedDailyReport;
ResultSet rs = stm.executeQuery(query); // The String "query" is a dynamic MySQL query.
while(rs.next())
{
if(reType == reportType.detailedDailyReport)
{
int count4 = 0;
Calendar c = Calendar.getInstance();
c.setTime(rs.getTime("hour"));
for(count4 = 0; count4 < name_db_data.length; count4 ++)
{
ArrayNode array_aux2 = mapper.createArrayNode();
array_aux2 = (ArrayNode) data_raw.get(name_db_data[count4]).get(hourFormatted.format(c.getTime()));
array_aux2.add(rs.getDouble(name_db_data[count4]));
}
}

Related

How to cast all columns of Spark dataset to string using Java

I have a dataset with so many columns and I want to cast all columns to the string using Java.
I tried below steps, I want to know if there is any better way to achieve this?
Dataset<Row> ds = ...;
JavaRDD<String[]> stringArrRDD = ds.javaRDD().map(row->{
int length = row.length();
String[] columns = new String[length];
for(int i=0; i<length;i++){
columns[i] = row.get(i) !=null? row.get(i).toString():"";
}
return columns;});
You can iterate over columns:
for (String c: ds.columns()) {
ds = ds.withColumn(c, ds.col(c).cast("string"));
}
If you want to use objects only:
import org.apache.spark.sql.types.*;
...
for (String c: ds.columns()) {
ds = ds.withColumn(c, ds.col(c).cast(DataTypes.StringType));
}

Use string values from an array as string variable to parse json - codenameone

I have a no. of checkboxes (20), what i did is if a user select any checkbox, its name is stored in an array (eg abc array below in code). The name of the string variable that stores the respective json is of the same name as of the checkbox. For eg if Checkbox "a" is clicked, string value "a" is stored in array and there is a string variable named "a" that stores the related json values. What I need is that if i pass the string value stored in array as InputStream is = new ByteArrayInputStream(abc.get(i).getBytes()), it should be used to parse the inputStream for json. But it gives NullPointerException since the string value "a" is not equal to string variable a. How can i solve this problem? I ran out of ideas here. Is there other ways to achieve what i want to do here?
code: String values of the selected checkboxes are stored in an array
String a = "[{\n"
+ "\t\t\t\"title\": \"title1\",\n"
+ "\t\t\t\"describ\": \"describ1\"\n"
+ "}]";
String b = "[{\n"
+ "\"title\": \"title2\",\n"
+ "\"describ\": \"describ2\"\n"
+ "}]";
String c = "[{\n"
+ "\t\t\t\"title\": \"title3\",\n"
+ "\t\t\t\"describ\": \"describ3\"\n"
+ "}]";
//and all jsons required are there
ArrayList<String> abc;
#Override
protected void beforeTestForApp(Form f) {
f.setTitle("abc");
abc = new ArrayList<>();
//I have stored "a" & "b" in the abc array here for simplicity, but it is dynamic,
//ie. if the user select checkbox c, c will be stored in abc array and so on
abc.add("a");
abc.add("b");
Button bb = new Button("go");
bb.addActionListener((e) -> {
showForm("TestForAppResult", null);
});
f.add(bb);
}
Form for json parser and displaying the values:
#Override
protected void beforeTestForAppResult(Form f) {
f.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
InputStream is;
for (int i = 0; i < abc.size(); i++) {
Label heading = new Label(abc.get(i));
f.add(heading);
//this gives error since abc.get(i) gives string value, not string variable
is = new ByteArrayInputStream(abc.get(i).getBytes());
showDetails(is, f);
}
//if i do this instead of for loop jst above, i can get the result but since what value'll be stored in an array is not known,it is not possible
//is = new ByteArrayInputStream(a.getBytes());
//showDetails(is, f);
//is = new ByteArrayInputStream(b.getBytes());
//showDetails(is, f);
}
private void showDetails(InputStream is, Form f) {
JSONParser p = new JSONParser();
Hashtable<String, Object> test;
try {
test = p.parse(new InputStreamReader(is));
Vector aVector = (Vector) test.get("root");
for (int j = 0; j < aVector.size(); j++) {
Hashtable hm = (Hashtable) aVector.get(j);
String title = (String) hm.get("title");
String describ = (String) hm.get("describ");
Label z = new Label(title);
Label zz = new Label(describ);
f.add(z);
f.add(zz);
}
} catch (IOException ex) {
}
}
tbh i didnt get your problem concretely but i still try to give you some shots so you can try out.
If i understand correctly you have 20 objects which contains values underlying?
So then you have a JSONArray, just iterate trough it and grab that JSONObject.
now just use parseJSON instead of parse as it is deprecated...
here is a short snippet of my code
JSONArray jsonTasks = new JSONArray(responseString);
for (int index = 0; index < jsonTasks.length(); index++) {
JSONObject jsonObject = (JSONObject) jsonTasks.get(index);
if (jsonObject != null) {
Map jsonMap = new JSONParser().parseJSON(new InputStreamReader(new ByteArrayInputStream(jsonObject.toString().getBytes(UTF8)), UTF8));
System.out.println(jsonMap.get("date"));

Loop Array to Plot Marker

I have a JSON with an array of 3 objects. So when I retrieveEventJSON(), I am simply setting the attributes to an Event object. And when I call the plotEventOnMap() from another activity, I expect to see 3 markers on the map.
public void retrieveEventJSON() throws JSONException {
String page;
JSONArray jsonArray;
try {
// Code to retrieve data from servlet
try {
JSONObject jsonObject = new JSONObject(page);
jsonArray = jsonObject.getJSONArray("Events");
int length = jsonArray.length();
for (int i = 0; i < length; i++) {
JSONObject attribute = jsonArray.getJSONObject(i);
String eventID = attribute.getString("eventID");
String eventName = attribute.getString("eventName");
String eventDesc = attribute.getString("eventDesc");
String eventDate = attribute.getString("eventDate");
eventModel.setEventID(eventID);
eventModel.setEventName(eventName);
eventModel.setEventDesc(eventDesc);
eventModel.setEventDate(eventDate);
}
} catch (JSONException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void plotEventOnMap(Context context) {
graphicIcon = new PictureMarkerSymbol(res);
Point p = new Point(Double.parseDouble(eventModel.getEventX()),
Double.parseDouble(eventModel.getEventY()));
Symbol symbol = graphicIcon;
HashMap<String, Object> attrMap = new HashMap<String, Object>();
attrMap.put("eventName", eventModel.getEventName());
attrMap.put("eventBy", eventModel.getEventBy());
ENeighbourhoodActivity.graphicsLayer.addGraphic(new Graphic(p, symbol,
attrMap));
}
But with these codes, it just display the last row of record in my JSON instead of looping and plot each of them. Any guides?
Thanks in advance.
You need to call .plotEventOnMap(), or otherwise do something with the EventModel you've constructed, from inside your loop, after setting all the EventModel properties. At the moment, you're just overwriting your EventModel without ever using it.
for (int i = 0; i < length; i++) {
JSONObject attribute = jsonArray.getJSONObject(i);
String eventID = attribute.getString("eventID");
String eventName = attribute.getString("eventName");
String eventDesc = attribute.getString("eventDesc");
String eventDate = attribute.getString("eventDate");
eventModel.setEventID(eventID);
eventModel.setEventName(eventName);
eventModel.setEventDesc(eventDesc);
eventModel.setEventDate(eventDate);
}
Just before the loop ends, you need to do something with the EventModel you've now constructed. This might be plotting it, or adding it to some collection, or whatever. But at the moment, you're going straight back into the loop, and then in the next iteration, overwriting all the good work you've done. The reason you're only ending up with the last one is that when you've done the last iteration, what's left in eventModel is what you wrote the last time you went through the loop.
In fact I think you also want
EventModel eventModel = new EventModel();
as the first thing inside your loop. (I don't know if this is exactly right because we haven't seen the code for EventModel so I don't know what the constructor looks like.) If you want to keep a List (or similar) of all of them, you need to make sure they're all different instances.
I would suggest recasting like this:
List<EventModel> events = new ArrayList<EventModel>(); //NEW
for (int i = 0; i < length; i++) {
EventModel eventModel = new EventModel(); //NEW
JSONObject attribute = jsonArray.getJSONObject(i);
String eventID = attribute.getString("eventID");
String eventName = attribute.getString("eventName");
String eventDesc = attribute.getString("eventDesc");
String eventDate = attribute.getString("eventDate");
eventModel.setEventID(eventID);
eventModel.setEventName(eventName);
eventModel.setEventDesc(eventDesc);
eventModel.setEventDate(eventDate);
events.add(eventModel); //NEW
}
After the loop has finished, you'll have a list of EventModels that you can send to your plotting method or whatever's appropriate.
eventModel.setEventName(eventName); returns the latest element added to it because on every iteration you are resetting it.
You may add the object to the list after each iteration so that the list will have the elements. Declare the your EventModel object inside the for loop to have the instance for every iteration.
for (int i = 0; i < length; i++) {
JSONObject attribute = jsonArray.getJSONObject(i);
String eventID = attribute.getString("eventID");
String eventName = attribute.getString("eventName");
EventModel eventModel=new eventModel();
eventModel.setEventID(eventID);
eventModel.setEventName(eventName);
list.add(eventModel);
}

How to convert rows of sqlite data to a ArrayList<double[]>()?

I get the data type of data from sqlite database is string. How to put these data into ArrayList()? Thank you!
try below code:-
ArrayList<String> array = new ArrayList<String>();
Levels l = new Levels(getBaseContext()); // level ur table name
l.open();
Cursor c_l = l.selectAll();
for (int i = 0; i < c_l.getCount()+1; i++)
{
array.add(c_l.getString(0)); // getstring 0 means your column
}
c_l.close();
l.close();

Array and date format issue

I am a beginer in jquery and java.I have two string variables and a string array.I passed these 3 to a java servlet using a post request from javascript.Below is the code:
var stDate= $('#drpstart').jqxDateTimeInput('getText');
var edDate= $('#drpend').jqxDateTimeInput('getText');
var items = $("#target").jqxListBox('getItems');
var itemsid=[];
$("#jqxWidget").html('');
for (var i = 0; i < items.length; i++) {
itemsid[i]=items[i].value;
}
$.post('ActionItem',
{startdt:stDate,enddt:edDate,item:itemsid},function(responseJson) {
});
Then I access these variables and array at servlet side:
String starts=request.getParameter("startdt");
String ends=request.getParameter("enddt");
String[] myArray =request.getParameterValues("item");
if (myArray != null ) {
for (int i = 0; i < myArray.length; i++){
System.out.println(myArray[i]);
}
}
else {
System.out.println("null value");
}
but the myarray is geting a null value.How to get the values correctly to myarray?
Also my database table StockRegisterHeader contain field 'Date' with Date as datatype,so i converted the string variables starts and ends to Date format as below
SimpleDateFormat from = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat to = new SimpleDateFormat("yyyy-MM-dd");
Date startg = from.parse(starts); // 01/02/2014
Date endg = from.parse(ends);
Now I need to select data from 4 tables within this date range. I wrote the query as below:
String ss="SELECT Items.ParentID,Items.Name ,StockRegisterHeader.MovementType,
StockRegisterHeader.Date, UOM.Name,StockRegisterDetails.Quantity,
StockRegisterDetails.Rate from Items,StockRegisterHeader,UOM,StockRegisterDetails
where Items.ID = StockRegisterDetails.ItemID and
StockRegisterDetails.StockRegisterHeaderID = StockRegisterHeader.Id and
StockRegisterDetails.UOMID = UOM.ID and StockRegisterHeader.Date between '"+startg+"'
and '"+endg+"'";
rs=stmt.executeQuery(ss);
But the resultset object rs is showing null even if the database contain the requested data.I think this may be due to the date format. How to solve these two issues? I am using mysql database. Please help me.
try changing
String[] myArray =request.getParameterValues("item");
to
String[] myArray =request.getParameterValues("item[]"); //brackets must be specified to retrive array value

Categories

Resources