2016-07-22 30 views
1

有這個JSON數據我如何解析JSON陣列到網頁視圖的Android

"posts":[{ 
"date":"2016-02-10 10:28:42", 
"categories":[{}], 
"tags":[], 
"author":{"name":"admin"}, 
"custom_fields":{ 
       "ref_number":["ITB NUMBER: ITB\/002\/2016"], 
       "deadline":["26 February, 2016"], 
       "entity":["Refugees (xxx)"] 
       } 

我想通過entity用下面的代碼在我JSONParser.java

  Post post = new Post(); 
      // Configure the Post object 
      post.setTitle(postObject.optString("title")); 


      post.setDate(postObject.optString("date", "N/A")); 
      post.setContent(postObject.optString("content", "N/A")); 
post.setCfs(postObject.getJSONObject("custom_fields").optJSONArray("entity").getString(0)); 

網頁視圖。在我PostFragment.java

id = args.getInt("id"); 

       //Title and date pass successfully 
       title = args.getString("title"); 
       String date = args.getString("date"); 

       //but the entity displays null 
       entity = args.getString("entity"); 

       //author is passed and it displays well/successfully 
       String author = args.getString("author"); 

       // Construct HTML content 

       // html for entity to webview 
       html += "<h2>" + entity + "</h2>"; 

       html += "<h3>" + title + "</h3>"; 
       // The actual content 
       html += content; 

時能夠通過與顯示titlecontent,使用下面的代碼然而,當我嘗試entity它顯示null中的WebView

我要去哪裏錯了

回答

0

嘗試:

entity = args.getString("custom_fields.entity"); 

希望它有幫助!

+0

這沒有幫助的ith元素或者,還是顯示爲空 –

0

你的問題是不明確的,按照我的理解,你需要Refugee元素

JSON你已經張貼無效。

爲每entity我的理解元素也是JSONObject,所以解析器應該是這樣的

.optJSONArray("entity").getJSONObject(i); // i is poistion 

這會給你實體array