2013-03-25 96 views
0
呼吁不断

我想在我的应用程序解析JSON:错误在Java中

所以首先我创建不变类我的Android应用程序,这对的app.config约6个变量:

(类别:1)

public class Constants{ 

    // url to make request 
    public static String url = "http://server.com/"; 

    // JSON Node names 
    public static final String TAG_CONTACTS = "contacts"; 
    public static final String TAG_ID = "id"; 
    public static final String TAG_NAME = "name"; 
    public static final String TAG_EMAIL = "email"; 
    public static final String TAG_ADDRESS = "address"; 
    public static final String TAG_GENDER = "gender"; 

} 

现在我想,所以我说干就干,创建了不同的类中使用此不同的等级:

(等级:2)

public class ReadFiles{ 

     public void readConstant(){ 
     //appConfig is JSONArray 
     JSONArray appConfig = null; 
     // Creating JSON Parser instance 
     JSONParser jParser = new JSONParser(); 


     // getting JSON string from URL 
     JSONObject json = jParser.getJSONFromUrl(c.url); 

     try { 
      // Getting Array of Contacts 
      contacts = json.getJSONArray(Constants.TAG_CONTACTS); 

      // looping through All Contacts 
      for(int i = 0; i < contacts.length(); i++){ 
       JSONObject d = details.getJSONObject(i); 

       // Storing each json item in variable 
       String id = d.getString(Constants.TAG_ID); //Error: 
     //ERROR : The method getString(int) in the type JSONArray is not applicable for the arguments-(String) 
       String name = d.getString(Constants.TAG_NAME); 
       String email = d.getString(Constants.TAG_EMAIL); 
       String address = d.getString(Constants.TAG_ADDRESS); 
       String gender = d.getString(Constants.TAG_GENDER); 

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

我得到块上的错误:String name = c.getString(Constants.TAG_NAME); 我想通过解析json将常量值应用于局部变量。

我跟着日食的提示,也还尝试做

String name = c.(Constants.TAG_NAME); 

但仍没有运气。这个区块有什么问题?我如何将json值分配给局部变量? 为了你的知识:这是我试图实现的:android-json-parsing-tutorial 但我想保持在不同的班级不断。

更新:所以我做了你建议的改变,我也得到新的错误:
String tabTitle = appConfig.(ConfigConstants.TITLE); //Error: Syntax error on token ".", Identifier expected after this token

+1

您试图通过类实例访问静态成员,你sh应该使用类名访问静态成员。 – Habib 2013-03-25 06:27:44

+0

检查我的答案更新,以便更新+清理代码并将代码粘贴到使用tabTitle的地方。目前的问题与此无关。 – SudoRahul 2013-03-25 06:48:45

回答

5

为什么不干脆用这个?

Constants.TAG_NAME 

为什么要实例化Constants类?由于Constants类别static中的所有字段,因此只需要使用ClassName常量)以static的方式访问它们。

更新: -

appConfig.(ConfigConstants.TITLE); 

不应该有一种方法?像这样的东西。

appConfig.someMethodName(ConfigConstants.TITLE); 
+0

嗨,我做了更改,并做了:'字符串tabTitle = appConfig.getString(ConfigConstants.TITLE);'但仍然出现错误。 – star18bit 2013-03-25 06:35:55

+0

这是一个奇怪的代码。 'for(int i = 0; i SudoRahul 2013-03-25 06:38:47

+1

'd'是一个'JSONObject',它为getString()方法提供了一个'String'。 – SudoRahul 2013-03-25 06:40:54

0

我想你正在使用JSONArray类在包

org.json.JSONArray 

其具有方法

给出该方法接受的整数参数
getString(int) 

,如果传递字符串参数,这是显而易见的错误通过Eclipse

+0

mmm yup you right,so how can I \t \t \t'字符串tabTitle = appConfig。(ConfigConstants.TITLE);' 这意味着将三个变量连接成一个变量 – star18bit 2013-03-25 06:38:50

+0

使用“contacts”而不是Constants.TAG_CONTACTS,对其他变量也是如此。你试一试 :) – Code2Interface 2013-03-25 06:47:58