使用assets文件夹进行简单的json解析(本地)

时间:2016-05-04 06:33:11

标签: java android eclipse

使用assets文件夹解析Json,但是当我运行应用程序时,xml布局没有显示已解析的数据。但它显示简单的布局而没有json解析。我该如何解决? 请给我一些建议。 下面是我的logcat信息。

public class MainActivity extends Activity {
static String TAG = "MainActivity";
TextView text1,text2,text3,text4,text5,text6;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    text1=(TextView)findViewById(R.id.text2);
    text2=(TextView)findViewById(R.id.text3);
    text3=(TextView)findViewById(R.id.text4);
    text4=(TextView)findViewById(R.id.text5);
    text5=(TextView)findViewById(R.id.text6);
    try{
    JSONObject obj= new JSONObject(loadJSONFromAsset());
    Log.e(TAG, "text1 :: " + obj.optString("dpfid"));
    Log.e(TAG, "text2 :: " + obj.optString("version"));
    Log.e(TAG, "text3 :: " + obj.optString("lastupdateon"));
    Log.e(TAG, "text4 :: " + obj.optString("lastsyncdate"));
    Log.e(TAG, "text5 :: " + obj.optString("emailid"));
    text1.setText(obj.optString("dpfid"));
    text2.setText(obj.optString("version"));
    text3.setText(obj.optString("lastupdatedon"));
    text4.setText(obj.optString("lastsyncdate"));
    text5.setText(obj.optString("emailid"));
}catch(Throwable e){
    Log.e(TAG,"Error e",e);
    e.printStackTrace();
    }
}
public String loadJSONFromAsset(){
    String json=null;
    try{
        InputStream is=MainActivity.this.getAssets().open("config.json");
        int size = is.available();
        byte[]buffer=new byte[size];
        is.read(buffer);
        is.close();
        json =new String(buffer,"UTF-8");
    }catch(IOException e){
        Log.e(TAG,":e",e);
    return null;
    }
    return json;
}

资产文件夹信息

 {
"dpfid": "sample-124-id",
"emailid": "abc@gmail.com",
"cmfoldername": "abc_at_gmail.com",
"version": "4.4",
"bulid": "abc",
"configscreeninterval": 2,
"pullrequestinterval": 60,
"width": 400,
"height": 300,
"storagefree": 1024,
"lastupdatedon": "2016-02-14 14:20"
"lastsyncdate": "2016-02-14 14:20"
 }

Logcat信息

05-02 13:29:24.922: E/MainActivity(3377):    Error e
05-02 13:29:24.922: E/MainActivity(3377):   "dpfid": "sample-124-id",
05-02 13:29:24.922: E/MainActivity(3377):   "emailid": "abc@gmail.com",
05-02 13:29:24.922: E/MainActivity(3377):   "cmfoldername":"abc_at_gmail.com",
05-02 13:29:24.922: E/MainActivity(3377):   "version": "4.4",
05-02 13:29:24.922: E/MainActivity(3377):   "bulid": "abc",
05-02 13:29:24.922: E/MainActivity(3377):   "configscreeninterval": 2,
05-02 13:29:24.922: E/MainActivity(3377):   "pullrequestinterval": 60,
05-02 13:29:24.922: E/MainActivity(3377):   "width": 400,
05-02 13:29:24.922: E/MainActivity(3377):   "height": 300,
05-02 13:29:24.922: E/MainActivity(3377):   "storagefree": 1024,
05-02 13:29:24.922: E/MainActivity(3377):   "lastupdatedon":"2016-02-15 14:20"
05-02 13:29:24.922: E/MainActivity(3377):   "lastsyncdate" :"2016-02-15 14:20"
05-02 13:29:24.922: E/MainActivity(3377):    }
05-02 13:29:24.922: E/MainActivity(3377):   at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
05-02 13:29:24.922: E/MainActivity(3377):   at org.json.JSONTokener.readObject(JSONTokener.java:394)
05-02 13:29:24.922: E/MainActivity(3377):   at org.json.JSONTokener.nextValue(JSONTokener.java:100)
05-02 13:29:24.922: E/MainActivity(3377):   at org.json.JSONObject.<init>(JSONObject.java:156)
05-02 13:29:24.922: E/MainActivity(3377):   at org.json.JSONObject.<init>(JSONObject.java:173)
05-02 13:29:24.922: E/MainActivity(3377):   at com.example.json.MainActivity.onCreate(MainActivity.java:40)
05-02 13:29:24.922: E/MainActivity(3377):   at android.app.Activity.performCreate(Activity.java:5937)
05-02 13:29:24.922: E/MainActivity(3377):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
05-02 13:29:24.922: E/MainActivity(3377):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
05-02 13:29:24.922: E/MainActivity(3377):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
05-02 13:29:24.922: E/MainActivity(3377):   at android.app.ActivityThread.access$800(ActivityThread.java:144)
05-02 13:29:24.922: E/MainActivity(3377):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
05-02 13:29:24.922: E/MainActivity(3377):   at android.os.Handler.dispatchMessage(Handler.java:102)
05-02 13:29:24.922: E/MainActivity(3377):   at android.os.Looper.loop(Looper.java:135)
05-02 13:29:24.922: E/MainActivity(3377):   at android.app.ActivityThread.main(ActivityThread.java:5221)
05-02 13:29:24.922: E/MainActivity(3377):   at java.lang.reflect.Method.invoke(Native Method)
05-02 13:29:24.922: E/MainActivity(3377):   at java.lang.reflect.Method.invoke(Method.java:372)
05-02 13:29:24.922: E/MainActivity(3377):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
05-02 13:29:24.922: E/MainActivity(3377):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

1 个答案:

答案 0 :(得分:2)

文件config.json有问题。

at“lastupdatedon”:“2016-02-14 14:20”缺少“,”

 {
"dpfid": "sample-124-id",
"emailid": "abc@gmail.com",
"cmfoldername": "abc_at_gmail.com",
"version": "4.4",
"bulid": "abc",
"configscreeninterval": 2,
"pullrequestinterval": 60,
"width": 400,
"height": 300,
"storagefree": 1024,
"lastupdatedon": "2016-02-14 14:20"
"lastsyncdate": "2016-02-14 14:20"
}

我解决了。试试这个!!!!

 {
"dpfid": "sample-124-id",
"emailid": "abc@gmail.com",
"cmfoldername": "abc_at_gmail.com",
"version": "4.4",
"bulid": "abc",
"configscreeninterval": 2,
"pullrequestinterval": 60,
"width": 400,
"height": 300,
"storagefree": 1024,
"lastupdatedon": "2016-02-14 14:20",
"lastsyncdate": "2016-02-14 14:20"
}