2014-05-15 69 views
0

从媒体服务器生成的JSON字符串看起来像这样解析大JSON数据与JSON数组和字符串值

{ 
    "id": 1, 
    "jsonrpc": "2.0", 
    "result": { 
     "albums": [ 
      { 
       "albumid": 1, 
       "albumlabel": "", 
       "artist": [ 
        "www.SongsLover.pk" 
       ], 
       "artistid": [ 
        2 
       ], 
       "description": "", 
       "genre": [ 
        "Pop" 
       ], 
       "label": "Single 2012", 
       "rating": 0, 
       "style": [ 
        "" 
       ], 
       "thumbnail": "image://[email protected]%3a%2f%2fCECOTS-NAS%2fMedia%2fMusic%2fbackup%20Music%2fSissle2%2fGangnam%20Style%20.mp3/", 
       "title": "Single 2012", 
       "type": "", 
       "year": 0 
      }, 
      { 
       "albumid": 164, 
       "albumlabel": "", 
       "artist": [ 
        "ARrahman","MJ" 
       ], 
       "artistid": [ 
        146,163 
       ], 
       "description": "", 
       "genre": [ 
        "Soundtrack" 
       ], 
       "label": "Lord of the rings", 
       "rating": 0, 
       "style": [ 
        "" 
       ], 
       "thumbnail": "image://[email protected]%3a%2f%2fCECOTS-NAS%2fMedia%2fMusic%2fExtras_Test%2fEnakkena%20Yenave.mp3/", 
       "title": "Lord of the rings", 
       "type": "", 
       "year": 2000 
      },{..........},{........},{........} 
     ], 
     "limits": { 
      "end": 155, 
      "start": 0, 
      "total": 155 
     } 
    } 
} 

以下是我试图使用Java的代码。 Iam得到json响应为输入流并使用jsonreader解析json响应。但是在上面的json中,艺术家字典具有没有名称的数组值。

JsonReader reader = new JsonReader(new InputStreamReader(inputStream, 
         "UTF-8")); 
ArrayList<Integer> albumId = new ArrayList<Integer>(); 
ArrayList<String> artistId = new ArrayList<String>(); 
    while (reader.hasNext()) { 
       String name = reader.nextName(); 
       if (name.equalsIgnoreCase("result")) { 
        reader.beginObject(); 
        while (reader.hasNext()) { 
         String check = reader.nextName(); 

         if (check.equalsIgnoreCase(type)) { 
          reader.beginArray(); 
          int i = 0; 
          while (reader.hasNext()) { 
           i++; 

            reader.beginObject(); 
            int albumid; 
            String artistid = null; 

            while (reader.hasNext()) { 

             name = reader.nextName(); 
             if (name.equalsIgnoreCase("albumid")) { 
              albumid = reader.nextInt(); 
              albumId.add(albumid); 
             } else if (name 
               .equalsIgnoreCase("artist")) { 
              reader.beginArray(); 
              while(reader.hasNext()){ 
               artistid = reader.nextString(); 
               artistId.add(artistid); 
              } 
              reader.endArray(); 
             } else { 
              reader.skipValue(); 
             } 
            } 

            reader.endObject(); 
           //} 
          } 
          Log.i(LOGCAT, Integer.toString(i)); 
          reader.endArray(); 
         } else { 
          reader.skipValue(); 
         } 
        } 

        reader.endObject(); 
       } else { 
        reader.skipValue(); 
       } 
      } 
      reader.endObject(); 

所以,对我的问题是如何从艺术家字典,上面的代码获取数组值。请帮助我。提前致谢。

+3

使用一个像样的JSON库(例如,杰克逊或Gson)并创建POJO;反序列化到这些POJO直接 – fge

+0

好吧,我会跟进。有没有办法用上面的代码实现输出? – user1906402

回答

0
// Try this way,hope this will you to solve your problem... 

     String respone = "{\"id\":1,\"jsonrpc\":\"2.0\",\"result\":{\"albums\":[{\"albumid\":1,\"albumlabel\":\"\",\"artist\":[\"www.SongsLover.pk\"],\"artistid\":[2],\"description\":\"\",\"genre\":[\"Pop\"],\"label\":\"Single 2012\",\"rating\":0,\"style\":[\"\"],\"thumbnail\":\"image://[email protected]%3a%2f%2fCECOTS-NAS%2fMedia%2fMusic%2fbackup%20Music%2fSissle2%2fGangnam%20Style%20.mp3/\",\"title\":\"Single 2012\",\"type\":\"\",\"year\":0},{\"albumid\":164,\"albumlabel\":\"\",\"artist\":[\"ARrahman\",\"MJ\"],\"artistid\":[146,163],\"description\":\"\",\"genre\":[\"Soundtrack\"],\"label\":\"Lord of the rings\",\"rating\":0,\"style\":[\"\"],\"thumbnail\":\"image://[email protected]%3a%2f%2fCECOTS-NAS%2fMedia%2fMusic%2fExtras_Test%2fEnakkena%20Yenave.mp3/\",\"title\":\"Lord of the rings\",\"type\":\"\",\"year\":2000}],\"limits\":{\"end\":155,\"start\":0,\"total\":155}}}"; 

     ArrayList<HashMap<String,Object>> albumList = new ArrayList<HashMap<String, Object>>(); 

     try{ 
      JSONObject responseJson = new JSONObject(respone); 
      JSONArray albumJsonArray = responseJson.getJSONObject("result").getJSONArray("albums"); 

      for (int i=0;i<albumJsonArray.length();i++){ 
       HashMap<String,Object> album = new HashMap<String, Object>(); 
       album.put("albumid",albumJsonArray.getJSONObject(i).getString("albumid")); 
       album.put("albumlabel",albumJsonArray.getJSONObject(i).getString("albumlabel")); 

       JSONArray artistJsonArray = new JSONArray(albumJsonArray.getJSONObject(i).getString("artist")); 
       ArrayList<String> artistList = new ArrayList<String>(); 
       for (int j=0;j<artistJsonArray.length();j++){ 
        artistList.add(artistJsonArray.getString(j)); 
       } 
       album.put("artist",artistList); 

       JSONArray artistidJsonArray = new JSONArray(albumJsonArray.getJSONObject(i).getString("artistid")); 
       ArrayList<String> artistidList = new ArrayList<String>(); 
       for (int j=0;j<artistidJsonArray.length();j++){ 
        artistidList.add(artistidJsonArray.getString(j)); 
       } 
       album.put("artistid",artistidList); 

       album.put("description",albumJsonArray.getJSONObject(i).getString("description")); 

       JSONArray genreJsonArray = new JSONArray(albumJsonArray.getJSONObject(i).getString("genre")); 
       ArrayList<String> genreList = new ArrayList<String>(); 
       for (int j=0;j<genreJsonArray.length();j++){ 
        genreList.add(genreJsonArray.getString(j)); 
       } 
       album.put("genre",genreList); 
       album.put("label",albumJsonArray.getJSONObject(i).getString("label")); 
       album.put("rating",albumJsonArray.getJSONObject(i).getString("rating")); 

       JSONArray styleJsonArray = new JSONArray(albumJsonArray.getJSONObject(i).getString("style")); 
       ArrayList<String> styleList = new ArrayList<String>(); 
       for (int j=0;j<styleJsonArray.length();j++){ 
        styleList.add(styleJsonArray.getString(j)); 
       } 
       album.put("style",styleList); 
       album.put("thumbnail",albumJsonArray.getJSONObject(i).getString("thumbnail")); 
       album.put("title",albumJsonArray.getJSONObject(i).getString("title")); 
       album.put("type",albumJsonArray.getJSONObject(i).getString("type")); 
       album.put("year",albumJsonArray.getJSONObject(i).getString("year")); 

       albumList.add(album); 
      } 


      for (HashMap<String,Object> album:albumList){ 
       System.out.println("Album Id : "+album.get("albumid").toString()); 
       System.out.println("Album Label : "+album.get("albumlabel").toString()); 
       System.out.println("Album Description : "+album.get("description").toString()); 
       System.out.println("Label : "+album.get("label").toString()); 
       System.out.println("Rating : "+album.get("rating").toString()); 
       System.out.println("Thumbnail : "+album.get("thumbnail").toString()); 
       System.out.println("Title : "+album.get("title").toString()); 
       System.out.println("Type : "+album.get("type").toString()); 
       System.out.println("Year : "+album.get("year").toString()); 
       System.out.println("Artist size : "+((ArrayList<String>)album.get("artist")).size()); 
       System.out.println("Artist Id size : "+((ArrayList<String>)album.get("artistid")).size()); 
       System.out.println("Genre size : "+((ArrayList<String>)album.get("genre")).size()); 
       System.out.println("Style size : "+((ArrayList<String>)album.get("style")).size()); 

      } 

     }catch (Throwable e){ 
      e.printStackTrace(); 
     } 
0

您可以使用JsonPath来提取艺术家数组。

例如“$ .result.albums [*]艺术家”。

JsonSurfer将是一个不错的选择,如果你正在处理大型JSON,因为它使用流解析器而不需要将整个JSON加载到内存中。

该代码与JsonSurfer一样短。

JsonSurfer jsonSurfer = JsonSurfer.gson(); 
Collection<Object> result = jsonSurfer.collectAll(inputStreamReader, "$.result.albums[*].artist");