无法将字符串转换为 JSON 数组并解析。

huangapple go评论71阅读模式
英文:

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

    [{
      &quot;ClientName&quot;:&quot;SELVI&quot;,
      &quot;AccountID&quot;:&quot;2040IG20000185&quot;,
      &quot;ToatalLonaAmount&quot;:&quot;35000.0000&quot;,
      &quot;RepaymentFequencyID&quot;:&quot;M&quot;,
      &quot;RepaymentFequency&quot;:&quot;Monthly&quot;,
      &quot;InterestRate&quot;:&quot;25.88&quot;,
      &quot;EMIAmount&quot;:&quot;1883.0000&quot;,
      &quot;PrincipleOutstanding&quot;:&quot;3626.0000&quot;,
      &quot;InterestOutstanding&quot;:&quot;108.0000&quot;,
      &quot;TotalTerm&quot;:&quot;24&quot;,
      &quot;RemainingTerm&quot;:&quot;2&quot;,
      &quot;ErrorCode&quot;:&quot;&quot;,
      &quot;Response&quot;:true,
      &quot;ResponseMsg&quot;:&quot;&quot;,
      &quot;Status&quot;:&quot;1&quot;,
      &quot;LoanStatusID&quot;:&quot;A&quot;,
      &quot;LoanStatus&quot;:&quot;Active Loan&quot;
    }]


how can i parse it and read values ?

please help me with this. it looks like json array i&#39;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 &lt; array.size(); i++) {
    	System.out.println(&quot;data :&quot;+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 = &#39;[{&quot;ClientName&quot;:&quot;SELVI&quot;,&quot;AccountID&quot;:&quot;2040IG20000185&quot;,&quot;ToatalLonaAmount&quot;:&quot;35000.0000&quot;,&quot;RepaymentFequencyID&quot;:&quot;M&quot;,&quot;RepaymentFequency&quot;:&quot;Monthly&quot;,&quot;InterestRate&quot;:&quot;25.88&quot;,&quot;EMIAmount&quot;:&quot;1883.0000&quot;,&quot;PrincipleOutstanding&quot;:&quot;3626.0000&quot;,&quot;InterestOutstanding&quot;:&quot;108.0000&quot;,&quot;TotalTerm&quot;:&quot;24&quot;,&quot;RemainingTerm&quot;:&quot;2&quot;,&quot;ErrorCode&quot;:&quot;&quot;,&quot;Response&quot;:true,&quot;ResponseMsg&quot;:&quot;&quot;,&quot;Status&quot;:&quot;1&quot;,&quot;LoanStatusID&quot;:&quot;A&quot;,&quot;LoanStatus&quot;:&quot;Active Loan&quot;}]&#39;

var objArr = JSON.parse(json);

for(var i=0; i &lt; objArr.length; i++) {
  console.log(&quot;Object from array: &quot;, 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 = &quot;ClientName&quot;)
    private String clientName;
    @JsonProperty(value = &quot;AccountID&quot;)
    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&lt;MyBindClass&gt; myBindClassList = new ObjectMapper().readValue(jsonString, new TypeReference&lt;List&lt;MyBindClass&gt;&gt;(){});

答案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());

huangapple
  • 本文由 发表于 2020年4月7日 20:38:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/61080218.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定