Advertisement
Atul_Kumar

JSON PARSER

Aug 6th, 2022
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.85 KB | None | 0 0
  1. JSON-PARSER
  2.  
  3. Program-6:
  4. Create two files of XML and JSON type with values for City_Name, Latitude,
  5. Longitude, Temperature, and Humidity. Develop an application to create an activity
  6. with two buttons to parse the XML and JSON files which when clicked should display
  7. the data in their respective layouts side by side.
  8. 1) Firstly, Create an Application by Name “JsonParser”
  9. 2) Go to xml code of design change the layout to “RelativeLayout”
  10. 3) Add TextView component & change the following properties:
  11. 1) Size: 38dp
  12. 2) Text: XML and JSON Parser
  13. 3) Center-Align
  14. 4) Add Two Buttons to Design & change the name “ParseXml” & “ParseJson” with following
  15. onclick functions:
  16. • ParseXml-Button: parsexml
  17. • ParseJson-Button: parsejson
  18. 5) Add TextView component & change the following properties:
  19. • Id: display
  20. • Text: “”
  21. • Align: Center
  22. 6) Add Assets folder by following the given hierarchy:
  23. App->new->folder->Assests folder
  24. 7) Inside the assets folder create new files of xml and json using the following hierarchy:
  25. new->file->city.xml
  26. new->file->city.json
  27. once created place the following details inside the “city.xml” and “city.json
  28. city.xml:
  29. <?xml version="1.0"?>
  30. <records>
  31.  <place>
  32.  <name>Mysore</name>
  33.  <lat>12.295</lat>
  34.  <long>76.639</long>
  35.  <temperature>22</temperature>
  36.  <humidity>90%</humidity>
  37.  </place>
  38.  <place>
  39.  <name>Bangalore</name>
  40.  <lat>13.295</lat>
  41.  <long>77.639</long>
  42. <temperature>25</temperature>
  43.  <humidity>74%</humidity>
  44.  </place>
  45. </records>
  46. city.json:
  47. [
  48.  {
  49.  "name":"HASSAN",
  50.  "lat":"12.295",
  51.  "long":"76.639",
  52.  "temperature":"22",
  53.  "humidity":"92%"
  54.  },
  55.  {
  56.  "name":"MANDYA",
  57.  "lat":"10.11",
  58.  "long":"66.639",
  59.  "temperature":"24",
  60.  "humidity":"82%"
  61.  }
  62. ]
  63. XML-CODE:
  64. <?xml version="1.0" encoding="utf-8"?>
  65. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  66.  xmlns:app="http://schemas.android.com/apk/res-auto"
  67.  xmlns:tools="http://schemas.android.com/tools"
  68.  android:layout_width="match_parent"
  69.  android:layout_height="match_parent"
  70.  tools:context=".MainActivity">
  71.  <TextView
  72.  android:id="@+id/textView"
  73.  android:layout_width="257dp"
  74. android:layout_height="59dp"
  75.  android:layout_alignParentEnd="true"
  76.  android:layout_alignParentBottom="true"
  77.  android:layout_marginEnd="74dp"
  78.  android:layout_marginBottom="453dp"
  79.  android:text="PARSER"
  80.  android:textSize="36sp"
  81.  tools:layout_editor_absoluteX="194dp"
  82.  tools:layout_editor_absoluteY="126dp" />
  83.  <Button
  84.  android:id="@+id/button"
  85.  android:layout_width="wrap_content"
  86.  android:layout_height="wrap_content"
  87.  android:layout_alignParentEnd="true"
  88.  android:layout_alignParentBottom="true"
  89.  android:layout_marginEnd="260dp"
  90.  android:layout_marginBottom="371dp"
  91.  android:backgroundTint="#F1B763"
  92.  android:onClick="parsexml"
  93.  android:text="XML"
  94.  android:textAlignment="center" />
  95.  <Button
  96.  android:id="@+id/button2"
  97.  android:layout_width="wrap_content"
  98.  android:layout_height="wrap_content"
  99.  android:layout_alignParentEnd="true"
  100.  android:layout_alignParentBottom="true"
  101.  android:layout_marginEnd="118dp"
  102.  android:layout_marginBottom="373dp"
  103.  android:backgroundTint="#CDDC39"
  104.  android:onClick="parsejson"
  105.  android:text="JSON"
  106.  android:textAlignment="center" />
  107.  <TextView
  108.  android:id="@+id/display"
  109.  android:layout_width="402dp"
  110.  android:layout_height="332dp"
  111.  android:layout_alignParentEnd="true"
  112.  android:layout_alignParentBottom="true"
  113.  android:layout_marginEnd="9dp"
  114.  android:layout_marginBottom="11dp"
  115.  android:textAlignment="center"
  116.  android:textColor="#EF3A78" />
  117. </RelativeLayout>
  118.  
  119.  
  120. JAVA-CODE
  121. package com.example.parserapplication;
  122. import androidx.appcompat.app.AppCompatActivity;
  123. import android.os.Bundle;
  124. import android.view.View;
  125. import android.widget.TextView;
  126. import android.widget.Toast;
  127. import org.json.JSONArray;
  128. import org.json.JSONObject;
  129. import org.w3c.dom.Document;
  130. import org.w3c.dom.Element;
  131. import org.w3c.dom.Node;
  132. import org.w3c.dom.NodeList;
  133. import java.io.InputStream;
  134. import java.nio.charset.StandardCharsets;
  135. import javax.xml.parsers.DocumentBuilder;
  136. import javax.xml.parsers.DocumentBuilderFactory;
  137. public class MainActivity extends AppCompatActivity {
  138.  TextView display;
  139.  @Override
  140.  protected void onCreate(Bundle savedInstanceState) {
  141.  super.onCreate(savedInstanceState);
  142.  setContentView(R.layout.activity_main);
  143.  display=findViewById(R.id.display);
  144.  }
  145.  public void parsexml(View v){
  146.  try {
  147.  InputStream is=getAssets().open("city.xml");
  148.  DocumentBuilderFactory documentBuilderFactory =
  149. DocumentBuilderFactory.newInstance();
  150.  DocumentBuilder
  151. documentBuilder=documentBuilderFactory.newDocumentBuilder();
  152.  Document document=documentBuilder.parse(is);
  153.  StringBuilder stringBuilder=new StringBuilder();
  154.  stringBuilder.append("XML DATA");
  155.  stringBuilder.append("\n-------------");
  156.  NodeList nodeList=document.getElementsByTagName("place");
  157.  for(int i=0; i<nodeList.getLength();i++){
  158.  Node node = nodeList.item(i);
  159.  if(node.getNodeType()==Node.ELEMENT_NODE){
  160.  Element element = (Element)node;
  161.  stringBuilder.append("\n Name:").append(getValue("name",element));
  162.  stringBuilder.append("\n Latitude:").append(getValue("lat",element));
  163.  stringBuilder.append("\n Longitude:").append(getValue("long",element));
  164. stringBuilder.append("\n
  165. Temperature:").append(getValue("temperature",element));
  166.  stringBuilder.append("\n humidity").append(getValue("humidity",element));
  167.  stringBuilder.append("\n----------");
  168.  }
  169.  }
  170.  display.setText(stringBuilder.toString());
  171.  }
  172.  catch (Exception e){
  173.  e.printStackTrace();
  174.  Toast.makeText(MainActivity.this,"Error in reading XML
  175. FILE",Toast.LENGTH_LONG).show();
  176.  }
  177.  }
  178.  public void parsejson(View V){
  179.  String json;
  180.  StringBuilder stringBuilder = new StringBuilder();
  181.  try {
  182.  InputStream is = getAssets().open("city.json");
  183.  int size=is.available();
  184.  byte[] buffer=new byte[size];
  185.  is.read(buffer);
  186.  json = new String(buffer, StandardCharsets.UTF_8);
  187.  JSONArray jsonArray = new JSONArray(json);
  188.  stringBuilder.append("JSON DATA");
  189.  stringBuilder.append("\n--------");
  190.  for(int i=0;i<jsonArray.length();i++){
  191.  JSONObject jsonObject = jsonArray.getJSONObject(i);
  192.  stringBuilder.append("\n Name:").append(jsonObject.getString("name"));
  193.  stringBuilder.append("\n Latidue:").append(jsonObject.getString("lat"));
  194.  stringBuilder.append("\n Longitude:").append(jsonObject.getString("long"));
  195.  stringBuilder.append("\n
  196. Temperature:").append(jsonObject.getString("temperature"));
  197.  stringBuilder.append("\n Humidity:").append(jsonObject.getString("humidity"));
  198.  stringBuilder.append("\n----------");
  199.  }
  200.  display.setText(stringBuilder.toString());
  201.  is.close();
  202.  }
  203.  catch (Exception e){
  204.  e.printStackTrace();
  205.  Toast.makeText(MainActivity.this,"Error in reading JSON
  206. file",Toast.LENGTH_LONG).show();
  207.  }
  208. }
  209.  private String getValue(String tag,Element element){
  210. return
  211. element.getElementsByTagName(tag).item(0).getChildNodes().item(0).getNodeValue();
  212.  }
  213. }
  214.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement