I have a DataResponseDto.json
{
"data": [
{
"customRule": {
"code": null,
"executionType": "ON_SUCCESS",
"description": "Description",
"owners": null,
"type": "TWO",
"enabled": true,
"objectType": "TEST",
"syncObjectKPIs": null,
"inactive": false,
"responsible": null,
"id": "0AB58A47D3A64B56A6B74DA0E66935DD",
"embedded": true,
"value": null,
"variables": [],
"kafkaEventName": null,
"lastChanged": 1530091858490,
"createPerson": null,
"externalId": null,
"groups": null,
"eventType": "UPDATE",
"branches": null,
"executionOrder": null,
"createDateTime": null,
"cronExpression": null,
"udfMetaGroups": null,
"name": "Sample1",
"location": null,
"permissionsType": "USER",
"udfValues": null,
"conditions": null,
"actions": [
{
"name": "ChecklistInstance",
"parameters": {
"templateName": "checklist"
}
}
],
"syncStatus": "IN_CLOUD",
"executionLog": []
},
"customRule": {
"code": null,
"executionType": "ON_SUCCESS",
"description": "Description",
"owners": null,
"type": "TWO",
"enabled": true,
"objectType": "TEST",
"syncObjectKPIs": null,
"inactive": false,
"responsible": null,
"id": "5033296D138C45C385AC141E1157B4FE",
"embedded": true,
"value": null,
"variables": [],
"kafkaEventName": null,
"lastChanged": 1530091858490,
"createPerson": null,
"externalId": null,
"groups": null,
"eventType": "UPDATE",
"branches": null,
"executionOrder": null,
"createDateTime": null,
"cronExpression": null,
"udfMetaGroups": null,
"name": "Sample2",
"location": null,
"permissionsType": "USER",
"udfValues": null,
"conditions": null,
"actions": [
{
"name": "ChecklistInstance",
"parameters": {
"templateName": "checklist"
}
}
],
"syncStatus": "IN_CLOUD",
"executionLog": []
}
}],
"pageSize": 1,
"currentPage": 0,
"lastPage": 0,
"totalObjectCount": 1,
"truncated": false
}
And I have a class to map this JSON file.
public class DataResponseDto {
private List<Map> data;
private Integer pageSize;
private Integer currentPage;
private Integer lastPage;
private Long totalObjectCount;
private Boolean truncated;
// getter setter
...
}
Now I am using ObjectMapper to parse this JSON into a java class. It retrieved DataResponseDto with only 1 map in data. It should be 2 maps in data.
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
File file = new File(classLoader.getResource("mock/DataResponseDto.json").getFile());
ObjectMapper mapper = new ObjectMapper();
DataResponseDto dataResponseDto = mapper.readValue(file, DataResponseDto.class);
List<RuleDto> rules = dataResponseDto.getData().stream().map(m -> mapper.convertValue(m.get("customRule"), RuleDto.class)).collect(Collectors.toList());
I am getting rules.size() = 1, it should be 2
Your problem is with the private List<Map> data;
Map cannot have duplicate keys, consider using something else such as MultiKeyMap from apache commons (org.apache.commons.collections.map.MultiKeyMap).
You have duplicate keys customRule. Try to make customRules an array with the rules and re-run the code.
Related
I'm trying to extract the json response to a case class in my project, but I'm getting an error stating:
Error Message is : No usable value for httpCode Did not find value which can be converted into int
This happens If I call to the API and extract that, but when I explicitly set the json response to a json parser and when I extracted them, they did work well.
My json response is:
{
"httpCode": 200,
"statusCode": "S_QUERY_1000",
"description": "Operation successful",
"data": [
{
"id": "5de0e1b3b2613f221986ff79",
"serial": "36c51161f820c2bc5c5bb83ee08881bb",
"type": "DOOR_OPEN",
"deviceLabel": "Door Sensor 2",
"locationLabel": "Support",
"notificationIds": [
{
"type": "FIREBASE",
"id": "1575018930989-6611da04-e2fa-4402-b16a-09c8ad8be2d5"
},
{
"type": "IVR",
"id": "48eb911aa838b54a96e6402947c48db4"
}
],
"acknowledgedBy": null,
"acknowledgmentType": null,
"acknowledgedDeviceLabel": null,
"reason": null,
"description": null,
"processor": "alt_p_01",
"initiatedDateTime": "2019-11-29T09:15:31.027Z",
"acknowledgedDateTime": null,
"sirenOnTime": null,
"sirenOffTime": "2020-01-21T12:16:21.445+08:00",
"updatedDateTime": "2020-01-21T12:16:21.923+08:00",
"status": "PROCESSING"
},
{
"id": "5de0dcf7b2613f221986ff78",
"serial": "36c51161f820c2bc5c5bb83ee08881bb",
"type": "MOTION_DETECTED",
"deviceLabel": "Tri-Sensor 1",
"locationLabel": "Lunch Room",
"notificationIds": [
{
"type": "FIREBASE",
"id": "1575017719760-53662549-d482-4759-94d7-5ec7fdb29309"
},
{
"type": "IVR",
"id": "a76d9424328efc7084add0cc46bcaa28"
}
],
"acknowledgedBy": null,
"acknowledgmentType": null,
"acknowledgedDeviceLabel": null,
"reason": null,
"description": null,
"processor": "alt_p_01",
"initiatedDateTime": "2019-11-29T08:55:19.807Z",
"acknowledgedDateTime": null,
"sirenOnTime": null,
"sirenOffTime": null,
"updatedDateTime": "2019-11-29T08:58:23.775Z",
"status": "PROCESSING"
},
{
"id": "5de0dc4bb2613f221986ff77",
"serial": "36c51161f820c2bc5c5bb83ee08881bb",
"type": "DOOR_OPEN",
"deviceLabel": "Door Sensor 3",
"locationLabel": "Home",
"notificationIds": [
{
"type": "FIREBASE",
"id": "1575017547742-d018d984-ed70-4fda-9458-8d84288fca00"
},
{
"type": "IVR",
"id": "8896fee125ed12d9cfbc3de07d3dbc6a"
}
],
"acknowledgedBy": null,
"acknowledgmentType": null,
"acknowledgedDeviceLabel": null,
"reason": null,
"description": null,
"processor": "alt_p_01",
"initiatedDateTime": "2019-11-29T08:52:27.803Z",
"acknowledgedDateTime": null,
"sirenOnTime": null,
"sirenOffTime": null,
"updatedDateTime": "2019-11-29T08:55:29.705Z",
"status": "PROCESSING"
},
{
"id": "5de0dc36b2613f221986ff76",
"serial": "36c51161f820c2bc5c5bb83ee08881bb",
"type": "DOOR_OPEN",
"deviceLabel": "Door Sensor 2",
"locationLabel": "Support",
"notificationIds": [
{
"type": "FIREBASE",
"id": "1575017526822-87d03515-21ca-45d4-b3f9-f12988a4a5b5"
},
{
"type": "IVR",
"id": "d10ab1e5cc802e6367989b9e29840e32"
}
],
"acknowledgedBy": null,
"acknowledgmentType": null,
"acknowledgedDeviceLabel": null,
"reason": null,
"description": null,
"processor": "alt_p_01",
"initiatedDateTime": "2019-11-29T08:52:06.857Z",
"acknowledgedDateTime": null,
"sirenOnTime": null,
"sirenOffTime": null,
"updatedDateTime": "2019-11-29T08:55:08.682Z",
"status": "PROCESSING"
},
{
"id": "5de0dbdab2613f221986ff75",
"serial": "36c51161f820c2bc5c5bb83ee08881bb",
"type": "MOTION_DETECTED",
"deviceLabel": "Tri-Sensor 1",
"locationLabel": "Lunch Room",
"notificationIds": [
{
"type": "FIREBASE",
"id": "1575017434604-19461ead-3b17-4648-bc70-d01ec8c113a5"
},
{
"type": "IVR",
"id": "289d0495bcc937ab30000a8c0c2253f3"
}
],
"acknowledgedBy": null,
"acknowledgmentType": null,
"acknowledgedDeviceLabel": null,
"reason": null,
"description": null,
"processor": "alt_p_01",
"initiatedDateTime": "2019-11-29T08:50:34.635Z",
"acknowledgedDateTime": null,
"sirenOnTime": null,
"sirenOffTime": null,
"updatedDateTime": "2019-11-29T08:53:35.641Z",
"status": "PROCESSING"
},
{
"id": "5de0db0cb2613f221986ff74",
"serial": "36c51161f820c2bc5c5bb83ee08881bb",
"type": "MOTION_DETECTED",
"deviceLabel": "Tri-Sensor 5",
"locationLabel": "Lobby",
"notificationIds": [
{
"type": "FIREBASE",
"id": "1575017228218-85607147-9414-40d7-b7dc-e4e7acd717db"
},
{
"type": "IVR",
"id": "6cef5f5fde5f3209d845390db04bc402"
}
],
"acknowledgedBy": null,
"acknowledgmentType": null,
"acknowledgedDeviceLabel": null,
"reason": null,
"description": null,
"processor": "alt_p_01",
"initiatedDateTime": "2019-11-29T08:47:08.284Z",
"acknowledgedDateTime": null,
"sirenOnTime": null,
"sirenOffTime": null,
"updatedDateTime": "2019-11-29T08:50:11.561Z",
"status": "PROCESSING"
},
{
"id": "5de0db09b2613f221986ff73",
"serial": "36c51161f820c2bc5c5bb83ee08881bb",
"type": "DOOR_OPEN",
"deviceLabel": "Door Sensor 2",
"locationLabel": "Support",
"notificationIds": [
{
"type": "FIREBASE",
"id": "1575017225719-cd828cfb-2570-4e9d-a60e-e0714a14830b"
},
{
"type": "IVR",
"id": "9c30bf0ea2ed111788f72fdef8b3e295"
}
],
"acknowledgedBy": null,
"acknowledgmentType": null,
"acknowledgedDeviceLabel": null,
"reason": null,
"description": null,
"processor": "alt_p_01",
"initiatedDateTime": "2019-11-29T08:47:05.755Z",
"acknowledgedDateTime": null,
"sirenOnTime": null,
"sirenOffTime": null,
"updatedDateTime": "2019-11-29T08:50:08.541Z",
"status": "PROCESSING"
},
{
"id": "5de0da9bb2613f221986ff72",
"serial": "36c51161f820c2bc5c5bb83ee08881bb",
"type": "DOOR_OPEN",
"deviceLabel": "Door Sensor 2",
"locationLabel": "Support",
"notificationIds": [
{
"type": "FIREBASE",
"id": "1575017115516-15600a8b-f387-46c7-8aa8-6c2575c0ab42"
},
{
"type": "IVR",
"id": "41e45e6f791bee847e73ff083fce43c5"
}
],
"acknowledgedBy": null,
"acknowledgmentType": null,
"acknowledgedDeviceLabel": null,
"reason": null,
"description": null,
"processor": "alt_p_01",
"initiatedDateTime": "2019-11-29T08:45:15.547Z",
"acknowledgedDateTime": null,
"sirenOnTime": null,
"sirenOffTime": null,
"updatedDateTime": "2019-11-29T08:48:17.485Z",
"status": "PROCESSING"
},
{
"id": "5de0da5ab2613f221986ff71",
"serial": "36c51161f820c2bc5c5bb83ee08881bb",
"type": "DOOR_OPEN",
"deviceLabel": "Door Sensor 2",
"locationLabel": "Support",
"notificationIds": [
{
"type": "FIREBASE",
"id": "1575017050570-97586921-28d7-447d-b237-53e62e2f9c2c"
},
{
"type": "IVR",
"id": "1aa425cb42031e9f2eb028cb8ec16aed"
}
],
"acknowledgedBy": null,
"acknowledgmentType": null,
"acknowledgedDeviceLabel": null,
"reason": null,
"description": null,
"processor": "alt_p_01",
"initiatedDateTime": "2019-11-29T08:44:10.655Z",
"acknowledgedDateTime": null,
"sirenOnTime": null,
"sirenOffTime": null,
"updatedDateTime": "2019-11-29T08:47:14.454Z",
"status": "PROCESSING"
},
{
"id": "5de0da4bb2613f221986ff70",
"serial": "36c51161f820c2bc5c5bb83ee08881bb",
"type": "DOOR_OPEN",
"deviceLabel": "Door Sensor 2",
"locationLabel": "Support",
"notificationIds": [
{
"type": "FIREBASE",
"id": "1575017035919-35b2b84f-f11d-4bd3-9ce1-0a094cb57c8c"
},
{
"type": "IVR",
"id": "89c98b5781bd3e8df415230fc9ce19c5"
}
],
"acknowledgedBy": null,
"acknowledgmentType": null,
"acknowledgedDeviceLabel": null,
"reason": null,
"description": null,
"processor": "alt_p_01",
"initiatedDateTime": "2019-11-29T08:43:55.956Z",
"acknowledgedDateTime": null,
"sirenOnTime": null,
"sirenOffTime": null,
"updatedDateTime": "2019-11-29T08:46:59.435Z",
"status": "PROCESSING"
}
],
"meta": {
"currentPage": 1,
"nextPage": 2,
"numOfPages": 244,
"previousPage": 0
}
}
My case classes to handle this json response are:
case class Alerts(httpCode: String, statusCode: String, description: String, data: List[Data])
case class Data(id: String, serial: String, `type`: String, deviceLabel: String, locationLabel: String, notificationIds: List[NotificationIds]
, acknowledgedBy: String, acknowledgmentType: String, acknowledgedDeviceLabel: String, reason: String, description: String
, processor: String, initiatedDateTime: String, acknowledgedDateTime: String, sirenOnTime: String, sirenOffTime: String,
updatedDateTime: String, status: String)
case class NotificationIds(`type`: String, id: String)
The way I extract this is:
implicit val format = DefaultFormats
//this api cal returns the response I stated above
val url = ServerConfigs.thingHttpHost+ServerConfigs.thingPathForActiveAlerts+serial
val jsonResponse = scala.io.Source.fromURL(url).mkString
val resData = jsonResponse.extract[Alerts]
Please assist.
I would say that you have inconsistency there. As error message tells you, you are sending integer "httpCode": 200, but in case class, you expect String httpCode:String. Just change it to httpCode:Int and you are good to go.
I have a JSON array, in that I need to make match count using ParentCommentID and parse data into recyclerview using text view while counting i get number count only one position and same for other item also, any help.
{
"d": {
"results": [
{
"__metadata": {
"id": "e7433825-6771-4f5e-96c7-c4d2674d7764",
"uri": "",
"etag": "\"2\"",
"type": "SP.Data.InquiryDiscussionsListListItem"
},
"Author": {
"__metadata": {
"id": "f064775c-6161-4bdb-9f4d-8bc6a898d218",
"type": "SP.Data.UserInfoItem"
},
"Title": "Submitter1"
},
"Id": 1501,
"ID": 1501,
"Title": null,
"Created": "2019-06-06T04:15:17Z",
"ParentCommentID": "1439",
"Comment": "<div class=\"ExternalClass8C333A77B6564A53BED74CA1BA2D2A10\">
reply add for 009</div>",
"CommentType": null,
"CommentDocumentName": null,
"AppID": "1083",
"Role": "Customer"
},
{
"__metadata": {
"id": "e92f708f-93dc-4587-8c4f-5518ed24f360",
"uri": "",
"etag": "\"2\"",
"type": "SP.Data.InquiryDiscussionsListListItem"
},
"Author": {
"__metadata": {
"id": "209d2d9a-bb07-4064-aaa0-231ad881a80f",
"type": "SP.Data.UserInfoItem"
},
"Title": "Submitter1"
},
"Id": 1500,
"ID": 1500,
"Title": null,
"Created": "2019-06-06T04:14:55Z",
"ParentCommentID": "1439",
"Comment": "<div class=\"ExternalClass44B1A0BB4D314C57BEE20141BFF10491\">comment add for 009</div>",
"CommentType": null,
"CommentDocumentName": null,
"AppID": "1083",
"Role": "Customer"
},
{
"__metadata": {
"id": "ec112002-3132-4bc1-8f85-03fbd9fda11d",
"uri": "",
"etag": "\"2\"",
"type": "SP.Data.InquiryDiscussionsListListItem"
},
"Author": {
"__metadata": {
"id": "6e8ecb1d-4deb-4168-a8b4-a725abf8002a",
"type": "SP.Data.UserInfoItem"
},
"Title": "Sarada Devi Potti"
},
"Id": 1439,
"ID": 1439,
"Title": "Canceled",
"Created": "2019-06-03T09:32:34Z",
"ParentCommentID": null,
"Comment": "<div class=\"ExternalClass5E1BFEDC348C43719AD940E644E0E0B6\">sdaeadfasdf</div>",
"CommentType": "Public",
"CommentDocumentName": null,
"AppID": "1083",
"Role": "Budget Analyst"
}
]
}
}
My code is onbind method:
Map<String, Integer> commentsCountMap = new HashMap<>();
for(Result res : discussionsList) {
String parentCommentId = res.getParentCommentID();
// If the MyDocsResult has a parent comment
if(parentCommentId != null) {
// get the count for this parent comment (default to 0)
int nbCommentsForParent = commentsCountMap.getOrDefault(parentCommentId, 0);
// increment the count
nbCommentsForParent++;
// Update the Map with the new count
commentsCountMap.put(parentCommentId, nbCommentsForParent);
System.out.println(commentsCountMap);
Log.d("Count:",commentsCountMap.toString());
}
}
for example id 1439, having two ParentCommentID, so in that case for id 1439 there should be a count of two comments, I tried to use for in my
recyclerview adapter but its dose not work.
I think you need to run through the list of Results and when when you find a Result that has a parent comment, then you go in the map, get the parent comment, increment its count by one and stick it back in the Map:
Assuming your Result class is something like this:
class Result {
private String id;
private String parentCommentID;
public Result(String id, String parentCommentID) {
this.id = id;
this.parentCommentID = parentCommentID;
}
// GETTERS/SETTERS
}
And you have a discussion list with 3 Results
discussionsList = Arrays.asList(
new Result("1439", null),
new Result("1500", "1439"),
new Result("1801", "1439")
);
Like your case, 1439 has no parent and 1500 and 1501 both have 1439 as parent
Then you can do this kind of thing:
Map<String, Integer> commentsCountMap = new HashMap<>();
// Loop through the discussion Map
for(Result res : discussionsList) {
String parentCommentId = res.getParentCommentID();
// If the Result has a parent comment
if(parentCommentId != null) {
// get the count for this parent comment (default to 0)
int nbCommentsForParent = commentsCountMap.getOrDefault(parentCommentId, 0);
// increment the count
nbCommentsForParent++;
// Update the Map with the new count
commentsCountMap.put(parentCommentId, nbCommentsForParent);
}
}
System.out.println(commentsCountMap);
This outputs
{1439=2}
I'm parsing event webhooks, and for the invoice.payment_succeeded event I need to get the Invoice object, the Subscription object, and the Plan object:
if (event.getType().equals("invoice.payment_succeeded")) {
Invoice invoice = (Invoice) event.getData().getObject();
Subscription subscription = invoice.getSubscriptionObject();
Plan plan = subscription.getPlan();
The problem is that the Subscription object it's getting is null, so I can't get the plan, but in the webhook json, the invoice is clearly not null:
<com.stripe.model.Invoice#1862723626 id=in_1EAqJIJIKy5oOMbPbbnFDfhe> JSON: {
"id": "in_1EAqJIJIKy5oOMbPbbnFDfhe",
"object": "invoice",
"amount_due": 500,
"amount_paid": 500,
"amount_remaining": 0,
"application_fee": null,
"attempt_count": 1,
"attempted": true,
"auto_advance": false,
"billing": "charge_automatically",
"billing_reason": "subscription_create",
"charge": "ch_1EAqJIJIKy5oOMbPzJWZRmkB",
"created": 1551842448,
"currency": "usd",
"custom_fields": null,
"customer": "cus_EdkAtTWX48vPlL",
"date": 1551842448,
"default_source": null,
"deleted": null,
"description": null,
"discount": null,
"due_date": null,
"ending_balance": 0,
"finalized_at": 1551842448,
"footer": null,
"hosted_invoice_url": "https://pay.stripe.com/invoice/invst_Xk5QCVu0wZGe8yDKRjNku8do3v",
"invoice_pdf": "https://pay.stripe.com/invoice/invst_Xk5QCVu0wZGe8yDKRjNku8do3v/pdf",
"lines": {
"object": "list",
"data": [
{
"id": "sli_b7542154e3401d",
"object": "line_item",
"amount": 500,
"currency": "usd",
"description": "1 ? Service (at $5.00 / month)",
"discountable": true,
"livemode": false,
"metadata": {},
"period": {
"end": 1554520848,
"start": 1551842448
},
"plan": {
"id": "plan_EYQ3ShMp3aS5RZ",
"object": "plan",
"active": true,
"amount": 500,
"billing_scheme": "per_unit",
"created": 1550523572,
"currency": "usd",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {},
"nickname": "535281012935229482:540347090178670611",
"product": "prod_EYQ2LpVmAUc2Kh",
"tiers": null,
"tiers_mode": null,
"transform_usage": null,
"usage_type": "licensed",
"deleted": null,
"name": null,
"statement_description": null,
"statement_descriptor": null,
"trial_period_days": null
},
"proration": false,
"quantity": 1,
"subscription": "sub_Ee8abQjuYiFJXx",
"subscription_item": "si_Ee8aKEipOAG8hQ",
"type": "subscription"
}
],
"has_more": false,
"total_count": 1,
"url": "/v1/invoices/in_1EAqJIJIKy5oOMbPbbnFDfhe/lines",
"count": null,
"request_options": null,
"request_params": null
},
"livemode": false,
"metadata
First of all the value of the subscription attribute can be null, for example in the case of one-off invoices.
The whole Subscription object is not included in the Invoice object by default, only its identifier is. The Invoice object contained in the Event object follows the same rule. So you will need to retrieve the Subscription with its identifier as described here
if (event.getType().equals("invoice.payment_succeeded")) {
Invoice invoice = (Invoice) event.getData().getObject();
Subscription subscription = Subscription.retrieve(invoice.getSubscription());
}
The plan attribute will only be set if the subscription contains a single plan.
Note that you can "expand" Stripe objects when retrieving them. See the section Expanding Objects of the Stripe API Reference.
It states in the docs the data will only contain the relevant object for your webhook.
In an invoice's case, it's the invoice object only. Luckily that contains the subscription ID for you:
"subscription": "sub_Ee8abQjuYiFJXx",
You can use this ID to retrieve the data as
Subscription subscription = Subscription.retrieve("sub_Ee8abQjuYiFJXx");
I want to convert this web service string to JSONArray , but it seems to have a problem , although I've validated it.
[
{
"hireDate": null,
"homePhoneNumber": null,
"gender": null,
"city": null,
"mobileNumber": null,
"idNumber": 123,
"religion": null,
"leftOver": 1,
"annualVacations": 5,
"dob": null,
"name": null,
"rank": 0,
"id": 1,
"workingHours": [],
"email": "ayman.daraghmah#exalt.ps",
"managers": [],
"alternativeMobileNumber": null,
"activated": true,
"username": "ayman.daraghmah#exalt.ps"
},
{
"hireDate": null,
"homePhoneNumber": null,
"gender": null,
"city": null,
"mobileNumber": null,
"idNumber": 123,
"religion": null,
"leftOver": 1,
"annualVacations": 5,
"dob": null,
"name": null,
"rank": 0,
"id": 11,
"workingHours": [],
"email": "aymandaraghmah#gmail.com",
"managers": [],
"alternativeMobileNumber": null,
"activated": true,
"username": "aymandaraghmah#gmail.com"
}
]
My Code:
JSONArray js = new JSONArray(payload);
what should the format of payload be to create the JSONArray ?
Looking at the code it looks like it should work. Following sample worked for me (The JSON is taken as a string for testing).
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Main {
public static void main(String[] args) throws JSONException {
String test = "[{\"hireDate\": null,\"homePhoneNumber\": null,\"gender\": null,\"city\": null,\"mobileNumber\": null,\"idNumber\": 123,\"religion\": null,\"leftOver\": 1,\"annualVacations\": 5,\"dob\": null,\"name\": null,\"rank\": 0,\"id\": 1,\"workingHours\": [],\"email\": \"ayman.daraghmah#exalt.ps\",\"managers\": [],\"alternativeMobileNumber\": null,\"activated\": true,\"username\": \"ayman.daraghmah#exalt.ps\"},{\"hireDate\": null,\"homePhoneNumber\": null,\"gender\": null,\"city\": null,\"mobileNumber\": null,\"idNumber\": 123,\"religion\": null,\"leftOver\": 1,\"annualVacations\": 5,\"dob\": null,\"name\": null,\"rank\": 0,\"id\": 11,\"workingHours\": [],\"email\": \"aymandaraghmah#gmail.com\",\"managers\": [],\"alternativeMobileNumber\": null,\"activated\": true,\"username\": \"aymandaraghmah#gmail.com\"}]";
JSONArray jsonArray = new JSONArray(test);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject explrObject = jsonArray.getJSONObject(i);
System.out.println(explrObject.getString("username"));
}
}
}
I would recommend you to follow this link to get your problem solve.
http://www.java67.com/2016/10/3-ways-to-convert-string-to-json-object-in-java.html
I would like to merge all the below objects as a single object recursively. Every time I run the code for every iteration I receive a string object, I am storing them in a list. List looks like below:
bean [String1, String2, String3]. These three strings are to be merged as a single string object.
String1:
[code=100,
response=
{
"testObjects": [
{
"updated": [
{
"attributes": {},
"id": "123"
},
{
"attributes": {},
"id": "456"
}
],
"timeCheckQuery": null,
"query": null,
"message": null,
"error": null,
"deleted": null,
"agentId": null,
"added": null
}
],
"message": null,
"error": null
}
]
String2:
[code=100,
response=
{
"testObjects": [
{
"updated": [
{
"attributes": {},
"id": "789"
},
{
"attributes": {},
"id": "759"
}
],
"timeCheckQuery": null,
"query": null,
"message": null,
"error": null,
"deleted": null,
"agentId": null,
"added": null
}
],
"message": null,
"error": null
}
]
String3:
[code=100,
response=
{
"testObjects": [
{
"updated": [
{
"attributes": {},
"id": "242"
},
{
"attributes": {},
"id": "951"
}
],
"timeCheckQuery": null,
"query": null,
"message": null,
"error": null,
"deleted": null,
"agentId": null,
"added": null
}
],
"message": null,
"error": null
}
]
output:
[code=300,
response=
{
"testObjects": [
{
"updated": [
{
"attributes": {},
"id": "123"
},
{
"attributes": {},
"id": "456"
},
{
"attributes": {},
"id": "789"
},
{
"attributes": {},
"id": "759"
},
{
"attributes": {},
"id": "242"
},
{
"attributes": {},
"id": "951"
}
],
"timeCheckQuery": null,
"query": null,
"message": null,
"error": null,
"deleted": null,
"agentId": null,
"added": null
}
],
"message": null,
"error": null
}
]
You could serialize the first object as a json string, then append that string with next object serialized and so on.
The value of the 'updated' field seems to be a JsonArray Structure.
What you have to do is have a Global Array that adds the values(value of the 'updated' field) from all the responses into a single JsonArray.
Using Gson Library you can do it as follows
JsonArray jsonarray = new JsonArray();
jsonarray.addAll(jsonObject1.get("updated").getAsJsonArray());
jsonarray.addAll(jsonObject2.get("updated").getAsJsonArray());
jsonarray.addAll(jsonObject2.get("updated").getAsJsonArray());
Now you can use this JsonArray inside any object as you need as your requirement needs.