Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
902 views
in Technique[技术] by (71.8m points)

json - Android - how to parse JsonArray from string?

I am trying to parse a json array from json string but it always throws the exception data of type java.lang.String cannot be converted to JSONArray.

Please tell me if I make any mistake.

Thanks.

Here is my codes to get Json from server:

try {
                String url = String.format(<url here>, province.provinceCode2);
                HttpClient httpClient = getHttpClient();
                HttpGet httpGet = new HttpGet(url);
                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity entity = httpResponse.getEntity();
                final String result = EntityUtils.toString(entity);
                parseAndSaveJsonData(province, result);
            } catch (Exception e) {
                e.printStackTrace();
            }

here is codes to parse JsonArray:

String jsonString = <below json string>
JSONArray ja = new JSONArray(jsonString);

Here is my json string:

   [
   {
      "LotPrizes":[
         {
            "Prize":"Gi?itám",
            "Range":"50"
         },
         {
            "Prize":"Gi?ib?y",
            "Range":"264"
         },
         {
            "Prize":"Gi?isáu",
            "Range":"3654-5162-3097"
         },
         {
            "Prize":"Gi?in?m",
            "Range":"9739"
         },
         {
            "Prize":"Gi?it?",
            "Range":"97690-99274-32442-69432-04855-10132-17085"
         },
         {
            "Prize":"Gi?iba",
            "Range":"73745-13007"
         },
         {
            "Prize":"Gi?inhì",
            "Range":"05521"
         },
         {
            "Prize":"Gi?inh?t",
            "Range":"74870"
         },
         {
            "Prize":"Gi?iDB6",
            "Range":"878833"
         }
      ]
      },
 {
      "LotPrizes":[
         {
            "Prize":"Gi?itám",
            "Range":"50"
         },
         {
            "Prize":"Gi?ib?y",
            "Range":"264"
         },
         {
            "Prize":"Gi?isáu",
            "Range":"3654-5162-3097"
         },
         {
            "Prize":"Gi?in?m",
            "Range":"9739"
         },
         {
            "Prize":"Gi?it?",
            "Range":"97690-99274-32442-69432-04855-10132-17085"
         },
         {
            "Prize":"Gi?iba",
            "Range":"73745-13007"
         },
         {
            "Prize":"Gi?inhì",
            "Range":"05521"
         },
         {
            "Prize":"Gi?inh?t",
            "Range":"74870"
         },
         {
            "Prize":"Gi?iDB6",
            "Range":"878833"
         }
      ]
      }

    ]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is how to initialize a JSON parser:

JSONObject jsonObject = new JSONObject(jsonString);

That will give you the entire string as a Json Object. From there, pull out an individual array as a JsonArray, like this:

JSONArray jsonArray = jsonObject.getJSONArray("LotPrizes");

To access each "LotPrizes" you can use for loop logic:

for(int i=0;i<jsonArray.length();i++)
{
  JSONObject curr = jsonArray.getJSONObject(i);

  prize = curr.getString("Prize")

//Do stuff with the Prize String here
//Add it to a list, print it out, etc.
}

EDIT: Final code after your JSON edit:

JSONArray jsonArray = null;
String jsonString = <your string>
String currPrize = null;

JSONObject jsonObject = new JSONObject(jsonString);

jsonArray = jsonObject.getJSONArray("data");

for(int i=0;i<jsonArray.length();i++)
{
    JSONArray currLot = jsonArray.getJSONObject(i);

    for(int j=0; j<currLot.length();j++)
    {
        JSONobject curr = currLot.getJSONObject(j);

        currPrize = curr.getString("Prize");

        //Do something with Prize
    }
}

This code is functional and I'm using an almost identical version in my code. Hope this (finally) works for you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...