gpt4 book ai didi

java - 如何解析json中的对象?

转载 作者:行者123 更新时间:2023-12-01 18:07:26 24 4
gpt4 key购买 nike

我正在尝试解析 json,但它给我错误 java lang.string 无法转换为 jsonArray。这是我的 json 和错误:

Value {"MemberList":[{"MemberId":1,"FirmId":1,"MemberTypeId":1,
"MemberUserName":"test@example.com", "MemberName":"sth","UpdateDate":"\/Date(1421840040000)\/",
"LastLoginDate":"\/Date(1454995980000)\/",
"FirmTypeId":1,"FirmName":"Firm","FirmOfficialName":"Firm",
"FirmRowGuid":"sth","MemberRowGuid":"sth","AuthToken":"sth",
"IsRegistered":1}],"MemberPageList":null,"FirmModuleList":null,"Status":true,
"StatusCode":"OK"} of type java.lang.String cannot be converted to JSONArray

当我删除 MemberList header 并手动运行时,它工作正常,但我必须使用 header 解析它,我应该如何解析它。 userDetail 是我的 json 字符串:

JSONArray jsonArray = new JSONArray(userDetail);
member = new Member();
for (int i = 0; i < jsonArray.length(); i++) {

JSONObject jsonObject = jsonArray.getJSONObject(i);
member.MemberId = jsonObject.optInt("MemberId");
member.MemberTypeId = jsonObject.optInt("MemberTypeId");
member.FirmTypeId = jsonObject.optInt("FirmTypeId");
member.IsRegistered = jsonObject.optInt("IsRegistered");

member.MemberUserName = jsonObject.optString("MemberUserName");
member.MemberName = jsonObject.optString("MemberName");
member.UpdateDate = jsonObject.optString("UpdateDate");
member.LastLoginDate = jsonObject.optString("LastLoginDate");
member.FirmName = jsonObject.optString("FirmName");
member.FirmOfficialName = jsonObject.optString("FirmOfficialName");
member.FirmRowGuid = jsonObject.optString("FirmRowGuid");
member.MemberRowGuid = jsonObject.optString("MemberRowGuid");
member.AuthToken = jsonObject.optString("AuthToken");
}

Here is complete code but still not works

最佳答案

它是一个 json 对象而不是数组,因此需要更改,

JSONArray jsonArray = new JSONArray(userDetail);

JSONObject jsonObj= new JSONObject(userDetail);
JSONArray jsonArray = jsonObj.getJSONArray("MemberList");

要手动检查 json,请使用此 http://jsoneditoronline.org/

代码:

    JSONObject jsonObj= new JSONObject(userDetail);
JSONArray jsonArray = jsonObj.getJSONArray("MemberList");

for (int i = 0; i < jsonArray.length(); i++) {
member = new Member();
JSONObject jsonObject = jsonArray.getJSONObject(i);
member.MemberId = jsonObject.optInt("MemberId");
member.MemberTypeId = jsonObject.optInt("MemberTypeId");
member.FirmTypeId = jsonObject.optInt("FirmTypeId");
member.IsRegistered = jsonObject.optInt("IsRegistered");

member.MemberUserName = jsonObject.optString("MemberUserName");
member.MemberName = jsonObject.optString("MemberName");
member.UpdateDate = jsonObject.optString("UpdateDate");
member.LastLoginDate = jsonObject.optString("LastLoginDate");
member.FirmName = jsonObject.optString("FirmName");
member.FirmOfficialName = jsonObject.optString("FirmOfficialName");
member.FirmRowGuid = jsonObject.optString("FirmRowGuid");
member.MemberRowGuid = jsonObject.optString("MemberRowGuid");
member.AuthToken = jsonObject.optString("AuthToken");
}

关于java - 如何解析json中的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35286618/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com