无法在android中分割json响应

时间:2017-05-05 10:53:56

标签: java android json

我从服务器获得了json响应。这是我的json:

 {
  "result": 1,
  "IsActive": true,
  "Type": 0,
  "FirstName": "John",
  "roleId": 6300,
  "MinTotalPriceOrder": 0,
  "PhoneNumberConfirmed": false,
  "Cell": "09382665250",
  "StartDate": "2015-09-30T11:41:58",
  "DepotId": 393,
  "mlm": null,
  "revoked": false
}

现在我要分开它们。我的意思是我想把" FirstName"从String中的服务器或在textview中显示它。但它在textview中显示了整个json! 这是我的java代码:

 StringRequest strRequest = new StringRequest(Request.Method.POST, url,
                    new Response.Listener<String>()
                    {
                        @Override
                        public void onResponse(String response)
                        {
                            try {
                                JSONArray jArray = new JSONArray(response);
                                JSONObject jObject = jArray.getJSONObject(1);
                                String result = jObject.getString("FirstName");
                                textview.setText(result);

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

问题是什么?

2 个答案:

答案 0 :(得分:1)

试试这个

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:str="http://exslt.org/strings"
    extension-element-prefixes="str">
    <xsl:output method="xml" indent="yes" />

    <xsl:template match="file">
        <xsl:variable name="parts" select="str:tokenize(@name, '/')" />
        <file filename="{$parts[last()]}" folderName="{$parts[last() - 1]}/"/>
    </xsl:template>
</xsl:stylesheet>

答案 1 :(得分:0)

你需要通过JSONObject而不是JSONArray来获取它,因为那是json对象。请尝试以下代码。

StringRequest strRequest = new StringRequest(Request.Method.POST, url,
  new Response.Listener<String>()
  {
    @Override
    public void onResponse(String response)
    {
        try {
             JSONObject jObject = new JSONObject(response);

             String result = jObject.getString("result");
             String isActive =  jObject.getString("IsActive");
             String Type = jObject.getString("Type");
             String FirstName =  jObject.getString("FirstName");
             String roleId = jObject.getString("roleId");
             String MinTotalPriceOrder = jObject.getS tring("MinTotalPriceOrder");
             String PhoneNumberConfirmed = jObject.getString("PhoneNumberConfirmed");
             String Cell =  jObject.getString("Cell");
             String StartDate = jObject.getString("StartDate");
             String DepotId =  jObject.getString("DepotId");
             String mlm = jObject.getString("mlm");
             String revoked = jObject.getString("revoked");
             textview.setText(FirstName);

            } catch (JSONException e) {
                      e.printStackTrace();
            }
       }
}