从Android中的服务器接收的JSON字符串中获取数据

时间:2014-09-30 07:37:11

标签: android json parsing

我有一个像这样的JSON字符串,它是从服务器接收的:

[{"AdsId":"7","AdsName":"PIC_01.JPG","AdsImage":"pic_01.jpg","AdsImageUrl":"http://www.google.com","TargetUrl":"http://www.yahoo.com","IsAllPageSupport":false,"IsArabic":false,"IsActive":true},{"AdsId":"8","AdsName":"PIC_02.JPG","AdsImage":"pic_02.jpg","AdsImageUrl":"http://www.fb.com","TargetUrl":"http://www.twitter.com","IsAllPageSupport":true,"IsArabic":false,"IsActive":true},{"AdsId":"9","AdsName":"PIC_03.JPG","AdsImage":"pic_03.jpg","AdsImageUrl":"http://www.google.com","TargetUrl":"http://www.demo.com","IsAllPageSupport":false,"IsArabic":false,"IsActive":true},{"AdsId":"10","AdsName":"PIC_04.JPG","AdsImage":"pic_04.jpg","AdsImageUrl":"http://www.sample.com","TargetUrl":"http://www.example.com","IsAllPageSupport":true,"IsArabic":false,"IsActive":true}]

这是一种有效的JSON格式。但我不知道如何解析此字符串并检索此JSON字符串中的值。

我正在使用此代码来解析JSON数组。但是这段代码用于从JSON数组中检索值,我不知道如何从上面收到的JSON字符串中获取值。

JSONObject jsonResponse;

try {           
    jsonResponse = new JSONObject(status);

    JSONArray jsonMainNode = jsonResponse.optJSONArray("Android");

    int lengthJsonArr = jsonMainNode.length();

    Log.d("Json Array Length of status",String.valueOf(lengthJsonArr));

    for(int j1=0; j1 < lengthJsonArr; j1++) {

        JSONObject jsonChildNode = jsonMainNode.getJSONObject(j1);                      
        String addstatus=jsonChildNode.optString("slno").toString();
    }
 } catch(Exception ex) {
     ex.printStackTrace();
 }

3 个答案:

答案 0 :(得分:2)

试试此代码

String s = "[{'AdsId':'7','AdsName':'PIC_01.JPG','AdsImage':'pic_01.jpg','AdsImageUrl':'http://www.google.com','TargetUrl':'http://www.yahoo.com','IsAllPageSupport':false,'IsArabic':false,'IsActive':true},{'AdsId':'8','AdsName':'PIC_02.JPG','AdsImage':'pic_02.jpg','AdsImageUrl':'http://www.fb.com','TargetUrl':'http://www.twitter.com','IsAllPageSupport':true,'IsArabic':false,'IsActive':true},{'AdsId':'9','AdsName':'PIC_03.JPG','AdsImage':'pic_03.jpg','AdsImageUrl':'http://www.google.com','TargetUrl':'http://www.demo.com','IsAllPageSupport':false,'IsArabic':false,'IsActive':true},{'AdsId':'10','AdsName':'PIC_04.JPG','AdsImage':'pic_04.jpg','AdsImageUrl':'http://www.sample.com','TargetUrl':'http://www.example.com','IsAllPageSupport':true,'IsArabic':false,'IsActive':true}]";
        JSONArray array;
        try {
            array = new JSONArray(s);

            for (int i = 0; i < array.length(); i++) {
                Log.v(i + "AdsId", array.getJSONObject(i)
                        .getString("AdsId"));
                Log.v(i + "AdsName",
                        array.getJSONObject(i).getString("AdsName"));
            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

结果

0AdsId: 7
0AdsName: PIC_01.JPG
1AdsId: 8
1AdsName: PIC_02.JPG
2AdsId: 9
2AdsName): PIC_03.JPG
3AdsId: 10
3AdsName: PIC_04.JPG

答案 1 :(得分:0)

您应该创建JSONArray对象。

JSONArray ja = new JSONArray(satus); //check the array is not empty and execute ja.optJSONObject(0).optString("AdsId");

答案 2 :(得分:0)

简单方法?使用更高级别的JSON对象映射器,如Jackson(https://github.com/FasterXML/jackson):

ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
ArrayList<Map<String, Object>> list = mapper.readValue(status, ArrayList.class);
// You can now iterate over or randomly access the list
String adsName = list.get(0).get("AdsName");

如果您想在不添加自定义库的情况下执行此操作,则代码是一个良好的开端,但您在代码本身中存在许多问题。您对Logging的使用是错误的 - 第一个参数是日志标记(通常是类名),第二个参数是要记录的字符串。试试这个:

try {

  final JSONArray jsonArray = new JSONArray(inputString);

  final int lengthJsonArr = jsonArray.length();

  Log.d("JSONParse", "Json Array Length of status " + lengthJsonArr);

  for(int i = 0; i < lengthJsonArr; i++) {
    // This is an object from the array.
    JSONObject jsonChildNode = jsonArray.getJSONObject(i);
    // Retrieve any value from the object like this
    String adName = jsonChildNode.optString("AdsName").toString();
  }
} catch(Exception ex)
{
  Log.e("JSONParse", "Cannot read JSON string", ex);
}

熟悉在developer.android.com上使用Androids优秀的Javadocs,例如http://developer.android.com/reference/org/json/JSONArray.html