JSON Pasing - 怎么样?

时间:2017-11-29 19:10:34

标签: android arrays json

如果以jsonArray而不是jsonObject开头,我不知道如何解析JSON。 有JSON代码。

[
 {
   "id": 1,
   "title":
    {
     "rendered": "Apple apologises and fixes security flaw"
    }
 },

 {
   "id": 2,
   "title": 
    {
     "rendered": "Trophy hunting removes good genes and raises extinction risk"
    }
 }
   ...
]

我不知道如何获得JSONArray长度。如:

for (int i = 0; i < JSONArray.length(); i++)
{
  JSONObject JSONObject1 = JSONArray.getJSONObject(i);
  int id = JSONObject1.getInt("id");
  string title = JSONObject1.getString("rendered");
}

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:0)

尝试以下代码

import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;

public static JsonNode sendSimpleMessage() throws UnirestException {

    HttpResponse<JsonNode> request = Unirest
            .post("https://api.mailgun.net/v3/" + "sandbox21801cd223b94bbc92dbd011424xxxxx.mailgun.org"
                    + "/messages")
            .basicAuth("api", "xxx8013e6a2e75889370b0d2e670b2e")
            .queryString("from", "Excited User <notifications-noreply@miempresa.com>")
            .queryString("to", "joseluis.garcia@miempresa.com").queryString("subject", "hello")
            .queryString("text", "testing").asJson();

    return request.getBody();
}

答案 1 :(得分:0)

假设您的json值在此字符串变量中:public class UsersController : BaseController { // and any other services I need are here: private readonly IAnotherService svc; public UsersController(IAnotherService svc) { this.svc = svc; } ... }

json