我有一个JSON数组,我需要使用id进行匹配计数并将数据解析为文本视图
我试图为此使用一个for循环,但它似乎无法正常工作。我进行了很多搜索,但找不到适合的解决方案。例如,在一个对象中,我们有一个id,因此基于id的对象需要找到其他对象的明文id值,并计算计数以在文本视图中解析任何解决方案。
{
"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"
}
]
}
}
我的代码是onbind方法:
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onBindViewHolder(@NonNull DiscussingAdapter.DiscussionView holder, int position) {
ArrayList<String> minqtyList = new ArrayList<String>();
Result result = discussionsList.get(position);
holder.tvDetail.setText(Utils.html2text(result.getComment()));
holder.tvHeader.setText(result.getTitle());
holder.tvViewReply.setText("Reply");
holder.tvUser.setText("By " + result.getAuthor().getTitle());
String DateofReceipt = result.getCreated();
String date = DateofReceipt.split("T", 0)[0];
String date_before = date;
String date_after = Utils.date(date_before);
holder.tvDate.setText(date_after);
HashMap<String, Integer> hashMap = new HashMap<>();
int number = 0;
if (result.getParentCommentID() != null) {
for (int i = 0; i < result.getParentCommentID().length(); i++) {
if (result.getParentCommentID() != null) {
number++;
hashMap.put(result.getParentCommentID(), number);
}
}
}
}
例如id 1439
,有两个ParentCommentID
,因此,对于id 1439
,应该有两个注释,我尝试在我的recyclerview适配器中使用for它不起作用。
答案 0 :(得分:0)
我认为您需要遍历“结果”列表,当找到具有父注释的结果时,便进入地图,获取父注释,将其计数加1并将其粘贴回地图:
假设您的Result类是这样的:
class Result {
private String id;
private String parentCommentID;
public Result(String id, String parentCommentID) {
this.id = id;
this.parentCommentID = parentCommentID;
}
// GETTERS/SETTERS
}
您将获得一个包含3个结果的讨论列表
discussionsList = Arrays.asList(
new Result("1439", null),
new Result("1500", "1439"),
new Result("1801", "1439")
);
像您的情况一样,1439没有父母,而1500和1501都拥有1439作为父母
然后您可以执行以下操作:
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);
此输出
{1439=2}