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

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

Not able to convert a String to json Array and parse

问题

  1. 我遇到了关于字符串JSON转换和解析的问题可能看起来很傻但我束手无策
  2. 这是来自服务器的响应它是一个字符串
  3. [{
  4. "ClientName":"SELVI",
  5. "AccountID":"2040IG20000185",
  6. "ToatalLonaAmount":"35000.0000",
  7. "RepaymentFequencyID":"M",
  8. "RepaymentFequency":"Monthly",
  9. "InterestRate":"25.88",
  10. "EMIAmount":"1883.0000",
  11. "PrincipleOutstanding":"3626.0000",
  12. "InterestOutstanding":"108.0000",
  13. "TotalTerm":"24",
  14. "RemainingTerm":"2",
  15. "ErrorCode":"",
  16. "Response":true,
  17. "ResponseMsg":"",
  18. "Status":"1",
  19. "LoanStatusID":"A",
  20. "LoanStatus":"Active Loan"
  21. }]
  22. 我该如何解析它并读取其中的值
  23. 请帮我解决这个问题它看起来像是JSON数组我对此非常陌生
  24. 我尝试了以下代码但无法读取值
  25. ```java
  26. JSONParser parser = new JSONParser();
  27. Object obj1 = parser.parse(response.toString());
  28. JSONArray array = new JSONArray();
  29. array.add(obj1);
  30. //JSONArray jsonarray = new JSONArray();
  31. for (int i = 0; i < array.size(); i++) {
  32. System.out.println("data :" + array.get(i));
  33. }
  1. <details>
  2. <summary>英文:</summary>
  3. i have stuck with String ,json convertion and parsing . it may look silly but i am help less.
  4. here is my response from server which is string
  5. [{
  6. &quot;ClientName&quot;:&quot;SELVI&quot;,
  7. &quot;AccountID&quot;:&quot;2040IG20000185&quot;,
  8. &quot;ToatalLonaAmount&quot;:&quot;35000.0000&quot;,
  9. &quot;RepaymentFequencyID&quot;:&quot;M&quot;,
  10. &quot;RepaymentFequency&quot;:&quot;Monthly&quot;,
  11. &quot;InterestRate&quot;:&quot;25.88&quot;,
  12. &quot;EMIAmount&quot;:&quot;1883.0000&quot;,
  13. &quot;PrincipleOutstanding&quot;:&quot;3626.0000&quot;,
  14. &quot;InterestOutstanding&quot;:&quot;108.0000&quot;,
  15. &quot;TotalTerm&quot;:&quot;24&quot;,
  16. &quot;RemainingTerm&quot;:&quot;2&quot;,
  17. &quot;ErrorCode&quot;:&quot;&quot;,
  18. &quot;Response&quot;:true,
  19. &quot;ResponseMsg&quot;:&quot;&quot;,
  20. &quot;Status&quot;:&quot;1&quot;,
  21. &quot;LoanStatusID&quot;:&quot;A&quot;,
  22. &quot;LoanStatus&quot;:&quot;Active Loan&quot;
  23. }]
  24. how can i parse it and read values ?
  25. please help me with this. it looks like json array i&#39;m very new to this.
  26. i have tried this but not able to read values
  27. JSONParser parser = new JSONParser();
  28. Object obj1 = parser.parse(response.toString());
  29. JSONArray array = new JSONArray();
  30. array.add(obj1);
  31. //JSONArray jsonarray = new JSONArray();
  32. for (int i = 0; i &lt; array.size(); i++) {
  33. System.out.println(&quot;data :&quot;+array.get(i));
  34. }
  35. </details>
  36. # 答案1
  37. **得分**: 0
  38. 你可以简单地使用 JSON.parse(jsonValue) 进行操作;以下是示例代码:
  39. ```javascript
  40. 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"}]';
  41. var objArr = JSON.parse(json);
  42. for(var i=0; i < objArr.length; i++) {
  43. console.log("Object from array: ", objArr[i]);
  44. }
英文:

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 -->

  1. 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;
  2. var objArr = JSON.parse(json);
  3. for(var i=0; i &lt; objArr.length; i++) {
  4. console.log(&quot;Object from array: &quot;, objArr[i])
  5. }

<!-- end snippet -->

答案2

得分: 0

你可以使用 fasterxml.jackson 库将其简单地绑定到 POJO 中。只需创建一个类,其中的字段适合于 JSON 中的属性,例如:

  1. public class MyBindClass{
  2. @JsonProperty(value = "ClientName")
  3. private String clientName;
  4. @JsonProperty(value = "AccountID")
  5. private String accountId;
  6. // 等等
  7. // 这里是 getter 和 setter 方法
  8. }

然后你可以将你的 JSON 字符串绑定到这种对象的列表中(因为你的 JSON 包含了列表):

  1. 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.

  1. public class MyBindClass{
  2. @JsonProperty(value = &quot;ClientName&quot;)
  3. private String clientName;
  4. @JsonProperty(value = &quot;AccountID&quot;)
  5. private String accountId;
  6. //etc
  7. //getters and setters here
  8. }

and then you can bind your json string into list of such objects (as your json contains list):

  1. List&lt;MyBindClass&gt; myBindClassList = new ObjectMapper().readValue(jsonString, new TypeReference&lt;List&lt;MyBindClass&gt;&gt;(){});

答案3

得分: 0

收到以下解决方案:

  1. JSONParser parser = new JSONParser();
  2. Object obj1 = parser.parse(response.toString());
  3. JSONArray array = new JSONArray();
  4. array = (JSONArray) obj1;
  5. ResponseModel r = new Gson().fromJson(array.get(0).toString(), ResponseModel.class);
  6. System.out.println(r.getClientName());
英文:

got solution like below

  1. JSONParser parser = new JSONParser();
  2. Object obj1 = parser.parse(response.toString());
  3. JSONArray array = new JSONArray();
  4. array=(JSONArray) obj1;
  5. ResponseModel r= new Gson().fromJson(array.get(0).toString(), ResponseModel.class);
  6. 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:

确定