Home > Mobile >  In parsing json Mainactivity. Java file to add comments
In parsing json Mainactivity. Java file to add comments

Time:09-25

Package com. Example. Zuoye1;

The import android. OS. Bundle;
The import android. Widget. TextView;

The import androidx. Appcompat. App. AppCompatActivity;

The import org. Json. JSONException;
The import org. Json. JSONObject;

import java.io.IOException;
Import the Java. IO. InputStream;

Public class MainActivity extends AppCompatActivity {

@ Override
Protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
The setContentView (R.l ayout. Activity_main);
TextView text=(TextView) the findViewById (R.i which ext);

Try {
InputStream is=this. GetResources (). OpenRawResource (R.r aw. Test);
Byte [] buffer=new byte [is available ()];
Is. Read (buffer);
String json=new String (buffer, "utf-8");
JSONObject jsonObj=new JSONObject (json);//create a json object
String name=jsonObj. OptString (" name ");
Text. Append (" \ n "+ name +" \ n ");
Int the age=jsonObj. OptInt (" age ");
Text. Append (String. The valueOf (age) + "\ n");
Boolean I=jsonObj. OptBoolean (" I ");
Text. Append (String. The valueOf (" I "));
}
The catch (IOException e) {
e.printStackTrace();
} the catch (JSONException e) {
E.p rintStackTrace ();

}
}
}

CodePudding user response:

Have a local resource file R.r aw. Test, res raw directory, read the content, to bring their own JSONObject objects, and then read the value of the node

CodePudding user response:

reference 1st floor BDMH response:
there is a local resource file R.r aw. Test, res raw directory, read the content, to bring their own JSONObject objects, and then read the value of the node

Is for the code to add comments
  • Related