I have two json array one that is named http and another is websocketjsonArray , so there is one condition i haveto followup is inside nested for loop is that if their scripcode and userid is same then put and update data from websocketjsonarray into http array that's all i have to achieve please help me folks.
public class MainActivity extends AppCompatActivity {
private JSONArray http;
private JSONArray websocketjsonArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
http = new JSONArray("[{\"userid\":\"CNRM04\",\"groupname\":\"Rohit\",\n"
"\"segment\":\"CosPROFIN\",\"accountcode\":\"OWN\",\"symbol\":\"NIFTY\",\"exchange\":\"NSEFO\",\n"
"\"expirydate\":\"03NOV2022\",\"scripcode\":\"38021\",\"securitytype\":\"OPT\",\n"
"\"strikeprice\":17700,\"opttype\":\"PE\",\"bfqty\":-1500,\"bfrate\":122.35,\n"
"\"bfamt\":18352.5,\"buyqty\":0,\"sellqty\":0,\"netqty\":0,\"cfqty\":-150,\n"
"\"cfamt\":18352.5,\"ltp\":0,\"grossmtm\":0,\"brokerageamt\":0,\"netmtm\":0,\n"
"\"currency\":\"INR\",\"usdrate\":1,\"clientsharing\":0,\"broksharing\":0,\n"
"\"comsharing\":100,\"multiplier\":1},{\"userid\":\"CNRM04\",\"groupname\":\"Rohit\",\n"
"\"segment\":\"CosPROFIN\",\"accountcode\":\"OWN\",\"symbol\":\"NIFTY\",\n"
"\"exchange\":\"NSEFO\",\"expirydate\":\"03NOV2022\",\"scripcode\":\"38026\",\n"
"\"securitytype\":\"OPT\",\"strikeprice\":17800,\"opttype\":\"PE\",\"bfqty\":0,\n"
"\"bfrate\":0,\"bfamt\":0,\"buyqty\":10800,\"sellqty\":15000,\"netqty\":-4200,\n"
"\"cfqty\":-4200,\"cfamt\":590729.9999999998,\"ltp\":0,\"grossmtm\":0,\n"
"\"brokerageamt\":3400.2888479999992,\"netmtm\":0,\"currency\":\"INR\",\n"
"\"usdrate\":1,\"clientsharing\":0,\"broksharing\":0,\"comsharing\":100,\n"
"\"multiplier\":11},{\"userid\":\"CNC01\",\"groupname\":\"abhishek\",\n"
"\"segment\":\"Cosmic\",\"accountcode\":\"APP\",\"symbol\":\"NIFTY\",\n"
"\"exchange\":\"NSEFO\",\"expirydate\":\"03NOV2022\",\"scripcode\":\"40034\",\n"
"\"securitytype\":\"OPT\",\"strikeprice\":1200,\"opttype\":\"PE\",\"bfqty\":12,\n"
"\"bfrate\":2,\"bfamt\":0,\"buyqty\":100,\"sellqty\":121,\"netqty\":200,\n"
"\"cfqty\":11,\"cfamt\":78,\"ltp\":9,\"grossmtm\":0,\"brokerageamt\":12222,\n"
"\"netmtm\":45,\"currency\":\"INR\",\"usdrate\":2,\"clientsharing\":0,\"broksharing\":0,\"comsharing\":0,\n"
"\"multiplier\":99}]");
System.out.println("http " http);
websocketjsonArray = new JSONArray("[{\"userid\":\"CNRM04\",\"groupname\":\"Rohit\",\"segment\":\"CosPROFIN\",\"accountcode\":\"OWN\",\"symbol\":\"NIFTY\",\"exchange\":\"NSEFO\",\"expirydate\":\"03NOV2022\",\"scripcode\":\"38021\",\"securitytype\":\"OPT\",\"strikeprice\":17700,\"opttype\":\"PE\",\"bfqty\":20,\"bfrate\":122.35,\"bfamt\":18352.5,\"buyqty\":0,\"sellqty\":0,\"netqty\":0,\"cfqty\":-150,\"cfamt\":18352.5,\"ltp\":0,\"grossmtm\":0,\"brokerageamt\":0,\"netmtm\":0,\"currency\":\"INR\",\"usdrate\":1,\"clientsharing\":0,\"broksharing\":0,\"comsharing\":100,\"multiplier\":1},{\"userid\":\"CNRM04\",\"groupname\":\"Rohit\",\"segment\":\"CosPROFIN\",\"accountcode\":\"OWN\",\"symbol\":\"NIFTY\",\"exchange\":\"NSEFO\",\"expirydate\":\"03NOV2022\",\"scripcode\":\"38026\",\"securitytype\":\"OPT\",\"strikeprice\":17800,\"opttype\":\"PE\",\"bfqty\":30,\"bfrate\":0,\"bfamt\":0,\"buyqty\":10800,\"sellqty\":15000,\"netqty\":-4200,\"cfqty\":-4200,\"cfamt\":590729.9999999998,\"ltp\":0,\"grossmtm\":0,\"brokerageamt\":3400.2888479999992,\"netmtm\":0,\"currency\":\"INR\",\"usdrate\":1,\"clientsharing\":0,\"broksharing\":0,\"comsharing\":100,\"multiplier\":1}]]");
System.out.println("websocketarray " websocketjsonArray);
for (int k = 0; k < websocketjsonArray.length(); k ) {
for (int l = 0; l < http.length(); l ) {
if (http.getJSONObject(l).getString("scripcode").equalsIgnoreCase(websocketjsonArray.getJSONObject(k).getString("scripcode")) &&
http.getJSONObject(l).getString("userid").equalsIgnoreCase(websocketjsonArray.getJSONObject(k).getString("userid"))) {
String g = websocketjsonArray.getJSONObject(l).getString("bfqty");
http.put(Integer.parseInt("0"),"" g);
}
System.out.println("gfgfgf " http);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
i have tried iterating for loop with websocketjsonarray and inside that created another for loop for iterating http jsonarray and inside that one if conditon is there if that is satisfied than update json values from websocketjsonarray to http jsonarray one .
Expected desired output :
[{"userid":"CNRM04","groupname":"Rohit","segment":"CosPROFIN","accountcode":"OWN","symbol":"NIFTY","exchange":"NSEFO",
"expirydate":"03NOV2022","scripcode":"38021","securitytype":"OPT","strikeprice":17700,"opttype":"PE","bfqty":20,"bfrate":122.35,"bfamt":18352.5,"buyqty":0,"sellqty":0,
"netqty":0,"cfqty":-150, "cfamt":18352.5,"ltp":0,"grossmtm":0,"brokerageamt":0,"netmtm":0,"currency":"INR","usdrate":1,"clientsharing":0,"broksharing":0,"comsharing":100,"multiplier":1},
{"userid":"CNRM04","groupname":"Rohit","segment":"CosPROFIN","accountcode":"OWN","symbol":"NIFTY","exchange":"NSEFO","expirydate":"03NOV2022",
"scripcode":"38026","securitytype":"OPT","strikeprice":17800,"opttype":"PE","bfqty":30,"bfrate":0,"bfamt":0,"buyqty":10800,"sellqty":15000,"netqty":-4200,
"cfqty":-4200,"cfamt":590729.9999999998,"ltp":0,"grossmtm":0,"brokerageamt":3400.2888479999992,"netmtm":0,"currency":"INR",
"usdrate":1,"clientsharing":0,"broksharing":0,"comsharing":100,"multiplier":11},{"userid":"CNC01","groupname":"abhishek",
"segment":"Cosmic","accountcode":"APP","symbol":"NIFTY","exchange":"NSEFO","expirydate":"03NOV2022","scripcode":"40034","securitytype":"OPT",
"strikeprice":1200,"opttype":"PE","bfqty":12,"bfrate":2,"bfamt":0,"buyqty":100,"sellqty":121,"netqty":200,"cfqty":11,"cfamt":78,"ltp":9,
"grossmtm":0,"brokerageamt":12222,"netmtm":45,"currency":"INR","usdrate":2,"clientsharing":0,"broksharing":0,"comsharing":0,"multiplier":99}]");
As you can see that in jsonarray of first jsonobject which userid - CNRM04 & it's scripcode - 38021 their bfqty changed to 20 previously it is -1500 and after that in next jsonobject row userid - CNRM04 & it's scripcode - 38026 so for this also bfqty has changed to 30 value .So, the updated values of bfqty are changed from websocketjsonarray. So i finally want to update it like this.
CodePudding user response:
Library Josson & Jossons has dataset join operations.
https://github.com/octomix/josson
Jossons jossons = new Jossons();
// Deserialization
jossons.putDataset("http",
Josson.fromJsonString("[{\"userid\":\"CNRM04\",\"groupname\":\"Rohit\",\n"
"\"segment\":\"CosPROFIN\",\"accountcode\":\"OWN\",\"symbol\":\"NIFTY\",\"exchange\":\"NSEFO\",\n"
"\"expirydate\":\"03NOV2022\",\"scripcode\":\"38021\",\"securitytype\":\"OPT\",\n"
"\"strikeprice\":17700,\"opttype\":\"PE\",\"bfqty\":-1500,\"bfrate\":122.35,\n"
"\"bfamt\":18352.5,\"buyqty\":0,\"sellqty\":0,\"netqty\":0,\"cfqty\":-150,\n"
"\"cfamt\":18352.5,\"ltp\":0,\"grossmtm\":0,\"brokerageamt\":0,\"netmtm\":0,\n"
"\"currency\":\"INR\",\"usdrate\":1,\"clientsharing\":0,\"broksharing\":0,\n"
"\"comsharing\":100,\"multiplier\":1},{\"userid\":\"CNRM04\",\"groupname\":\"Rohit\",\n"
"\"segment\":\"CosPROFIN\",\"accountcode\":\"OWN\",\"symbol\":\"NIFTY\",\n"
"\"exchange\":\"NSEFO\",\"expirydate\":\"03NOV2022\",\"scripcode\":\"38026\",\n"
"\"securitytype\":\"OPT\",\"strikeprice\":17800,\"opttype\":\"PE\",\"bfqty\":0,\n"
"\"bfrate\":0,\"bfamt\":0,\"buyqty\":10800,\"sellqty\":15000,\"netqty\":-4200,\n"
"\"cfqty\":-4200,\"cfamt\":590729.9999999998,\"ltp\":0,\"grossmtm\":0,\n"
"\"brokerageamt\":3400.2888479999992,\"netmtm\":0,\"currency\":\"INR\",\n"
"\"usdrate\":1,\"clientsharing\":0,\"broksharing\":0,\"comsharing\":100,\n"
"\"multiplier\":11},{\"userid\":\"CNC01\",\"groupname\":\"abhishek\",\n"
"\"segment\":\"Cosmic\",\"accountcode\":\"APP\",\"symbol\":\"NIFTY\",\n"
"\"exchange\":\"NSEFO\",\"expirydate\":\"03NOV2022\",\"scripcode\":\"40034\",\n"
"\"securitytype\":\"OPT\",\"strikeprice\":1200,\"opttype\":\"PE\",\"bfqty\":12,\n"
"\"bfrate\":2,\"bfamt\":0,\"buyqty\":100,\"sellqty\":121,\"netqty\":200,\n"
"\"cfqty\":11,\"cfamt\":78,\"ltp\":9,\"grossmtm\":0,\"brokerageamt\":12222,\n"
"\"netmtm\":45,\"currency\":\"INR\",\"usdrate\":2,\"clientsharing\":0,\"broksharing\":0,\"comsharing\":0,\n"
"\"multiplier\":99}]"));
jossons.putDataset("websocket",
Josson.fromJsonString("[{\"userid\":\"CNRM04\",\"groupname\":\"Rohit\",\"segment\":\"CosPROFIN\",\"accountcode\":\"OWN\",\"symbol\":\"NIFTY\",\"exchange\":\"NSEFO\",\"expirydate\":\"03NOV2022\",\"scripcode\":\"38021\",\"securitytype\":\"OPT\",\"strikeprice\":17700,\"opttype\":\"PE\",\"bfqty\":20,\"bfrate\":122.35,\"bfamt\":18352.5,\"buyqty\":0,\"sellqty\":0,\"netqty\":0,\"cfqty\":-150,\"cfamt\":18352.5,\"ltp\":0,\"grossmtm\":0,\"brokerageamt\":0,\"netmtm\":0,\"currency\":\"INR\",\"usdrate\":1,\"clientsharing\":0,\"broksharing\":0,\"comsharing\":100,\"multiplier\":1},{\"userid\":\"CNRM04\",\"groupname\":\"Rohit\",\"segment\":\"CosPROFIN\",\"accountcode\":\"OWN\",\"symbol\":\"NIFTY\",\"exchange\":\"NSEFO\",\"expirydate\":\"03NOV2022\",\"scripcode\":\"38026\",\"securitytype\":\"OPT\",\"strikeprice\":17800,\"opttype\":\"PE\",\"bfqty\":30,\"bfrate\":0,\"bfamt\":0,\"buyqty\":10800,\"sellqty\":15000,\"netqty\":-4200,\"cfqty\":-4200,\"cfamt\":590729.9999999998,\"ltp\":0,\"grossmtm\":0,\"brokerageamt\":3400.2888479999992,\"netmtm\":0,\"currency\":\"INR\",\"usdrate\":1,\"clientsharing\":0,\"broksharing\":0,\"comsharing\":100,\"multiplier\":1}]]"));
// Left Join operation
JsonNode node = jossons.evaluateQuery(
"http{userid,scripcode} <=< websocket->map(userid,scripcode,bfqty){userid,scripcode}");
System.out.println(node.toPrettyString());
Output
[ {
"userid" : "CNRM04",
"groupname" : "Rohit",
"segment" : "CosPROFIN",
"accountcode" : "OWN",
"symbol" : "NIFTY",
"exchange" : "NSEFO",
"expirydate" : "03NOV2022",
"scripcode" : "38021",
"securitytype" : "OPT",
"strikeprice" : 17700,
"opttype" : "PE",
"bfqty" : 20,
"bfrate" : 122.35,
"bfamt" : 18352.5,
"buyqty" : 0,
"sellqty" : 0,
"netqty" : 0,
"cfqty" : -150,
"cfamt" : 18352.5,
"ltp" : 0,
"grossmtm" : 0,
"brokerageamt" : 0,
"netmtm" : 0,
"currency" : "INR",
"usdrate" : 1,
"clientsharing" : 0,
"broksharing" : 0,
"comsharing" : 100,
"multiplier" : 1
}, {
"userid" : "CNRM04",
"groupname" : "Rohit",
"segment" : "CosPROFIN",
"accountcode" : "OWN",
"symbol" : "NIFTY",
"exchange" : "NSEFO",
"expirydate" : "03NOV2022",
"scripcode" : "38026",
"securitytype" : "OPT",
"strikeprice" : 17800,
"opttype" : "PE",
"bfqty" : 30,
"bfrate" : 0,
"bfamt" : 0,
"buyqty" : 10800,
"sellqty" : 15000,
"netqty" : -4200,
"cfqty" : -4200,
"cfamt" : 590729.9999999998,
"ltp" : 0,
"grossmtm" : 0,
"brokerageamt" : 3400.2888479999992,
"netmtm" : 0,
"currency" : "INR",
"usdrate" : 1,
"clientsharing" : 0,
"broksharing" : 0,
"comsharing" : 100,
"multiplier" : 11
}, {
"userid" : "CNC01",
"groupname" : "abhishek",
"segment" : "Cosmic",
"accountcode" : "APP",
"symbol" : "NIFTY",
"exchange" : "NSEFO",
"expirydate" : "03NOV2022",
"scripcode" : "40034",
"securitytype" : "OPT",
"strikeprice" : 1200,
"opttype" : "PE",
"bfqty" : 12,
"bfrate" : 2,
"bfamt" : 0,
"buyqty" : 100,
"sellqty" : 121,
"netqty" : 200,
"cfqty" : 11,
"cfamt" : 78,
"ltp" : 9,
"grossmtm" : 0,
"brokerageamt" : 12222,
"netmtm" : 45,
"currency" : "INR",
"usdrate" : 2,
"clientsharing" : 0,
"broksharing" : 0,
"comsharing" : 0,
"multiplier" : 99
} ]