英文:
Not able to convert a String to json Array and parse
问题
我遇到了关于字符串、JSON转换和解析的问题。可能看起来很傻,但我束手无策。
这是来自服务器的响应,它是一个字符串:
[{
"ClientName":"SELVI",
"AccountID":"2040IG20000185",
"ToatalLonaAmount":"35000.0000",
"RepaymentFequencyID":"M",
"RepaymentFequency":"Monthly",
"InterestRate":"25.88",
"EMIAmount":"1883.0000",
"PrincipleOutstanding":"3626.0000",
"InterestOutstanding":"108.0000",
"TotalTerm":"24",
"RemainingTerm":"2",
"ErrorCode":"",
"Response":true,
"ResponseMsg":"",
"Status":"1",
"LoanStatusID":"A",
"LoanStatus":"Active Loan"
}]
我该如何解析它并读取其中的值?
请帮我解决这个问题。它看起来像是JSON数组,我对此非常陌生。
我尝试了以下代码,但无法读取值:
```java
JSONParser parser = new JSONParser();
Object obj1 = parser.parse(response.toString());
JSONArray array = new JSONArray();
array.add(obj1);
//JSONArray jsonarray = new JSONArray();
for (int i = 0; i < array.size(); i++) {
System.out.println("data :" + array.get(i));
}
<details>
<summary>英文:</summary>
i have stuck with String ,json convertion and parsing . it may look silly but i am help less.
here is my response from server which is string
[{
"ClientName":"SELVI",
"AccountID":"2040IG20000185",
"ToatalLonaAmount":"35000.0000",
"RepaymentFequencyID":"M",
"RepaymentFequency":"Monthly",
"InterestRate":"25.88",
"EMIAmount":"1883.0000",
"PrincipleOutstanding":"3626.0000",
"InterestOutstanding":"108.0000",
"TotalTerm":"24",
"RemainingTerm":"2",
"ErrorCode":"",
"Response":true,
"ResponseMsg":"",
"Status":"1",
"LoanStatusID":"A",
"LoanStatus":"Active Loan"
}]
how can i parse it and read values ?
please help me with this. it looks like json array i'm very new to this.
i have tried this but not able to read values
JSONParser parser = new JSONParser();
Object obj1 = parser.parse(response.toString());
JSONArray array = new JSONArray();
array.add(obj1);
//JSONArray jsonarray = new JSONArray();
for (int i = 0; i < array.size(); i++) {
System.out.println("data :"+array.get(i));
}
</details>
# 答案1
**得分**: 0
你可以简单地使用 JSON.parse(jsonValue) 进行操作;以下是示例代码:
```javascript
var json = '[{"ClientName":"SELVI","AccountID":"2040IG20000185","ToatalLonaAmount":"35000.0000","RepaymentFequencyID":"M","RepaymentFequency":"Monthly","InterestRate":"25.88","EMIAmount":"1883.0000","PrincipleOutstanding":"3626.0000","InterestOutstanding":"108.0000","TotalTerm":"24","RemainingTerm":"2","ErrorCode":"","Response":true,"ResponseMsg":"","Status":"1","LoanStatusID":"A","LoanStatus":"Active Loan"}]';
var objArr = JSON.parse(json);
for(var i=0; i < objArr.length; i++) {
console.log("Object from array: ", objArr[i]);
}
英文:
You can do it simply with JSON.parse(jsonValue);
Here is the example:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var json = '[{"ClientName":"SELVI","AccountID":"2040IG20000185","ToatalLonaAmount":"35000.0000","RepaymentFequencyID":"M","RepaymentFequency":"Monthly","InterestRate":"25.88","EMIAmount":"1883.0000","PrincipleOutstanding":"3626.0000","InterestOutstanding":"108.0000","TotalTerm":"24","RemainingTerm":"2","ErrorCode":"","Response":true,"ResponseMsg":"","Status":"1","LoanStatusID":"A","LoanStatus":"Active Loan"}]'
var objArr = JSON.parse(json);
for(var i=0; i < objArr.length; i++) {
console.log("Object from array: ", objArr[i])
}
<!-- end snippet -->
答案2
得分: 0
你可以使用 fasterxml.jackson
库将其简单地绑定到 POJO 中。只需创建一个类,其中的字段适合于 JSON 中的属性,例如:
public class MyBindClass{
@JsonProperty(value = "ClientName")
private String clientName;
@JsonProperty(value = "AccountID")
private String accountId;
// 等等
// 这里是 getter 和 setter 方法
}
然后你可以将你的 JSON 字符串绑定到这种对象的列表中(因为你的 JSON 包含了列表):
List<MyBindClass> myBindClassList = new ObjectMapper().readValue(jsonString, new TypeReference<List<MyBindClass>>(){});
英文:
you can simply bind it into POJO with fasterxml.jackson
library. Just create class with fields sutable to attributes in json, e.g.
public class MyBindClass{
@JsonProperty(value = "ClientName")
private String clientName;
@JsonProperty(value = "AccountID")
private String accountId;
//etc
//getters and setters here
}
and then you can bind your json string into list of such objects (as your json contains list):
List<MyBindClass> myBindClassList = new ObjectMapper().readValue(jsonString, new TypeReference<List<MyBindClass>>(){});
答案3
得分: 0
收到以下解决方案:
JSONParser parser = new JSONParser();
Object obj1 = parser.parse(response.toString());
JSONArray array = new JSONArray();
array = (JSONArray) obj1;
ResponseModel r = new Gson().fromJson(array.get(0).toString(), ResponseModel.class);
System.out.println(r.getClientName());
英文:
got solution like below
JSONParser parser = new JSONParser();
Object obj1 = parser.parse(response.toString());
JSONArray array = new JSONArray();
array=(JSONArray) obj1;
ResponseModel r= new Gson().fromJson(array.get(0).toString(), ResponseModel.class);
System.out.println(r.getClientName());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论