Retrieving and parsing json-type data

Read and parse json type data

1. What is JSON?

 

JSON(JavaScript Object Notation) is a lightweight data exchange format that uses a language-independent text format.
It is the ideal data exchange format, and JSON is the native JavaScript format.
Ideal for interaction between server and JavaScript
2. Why use JSON instead of XML

 

They all say this: Despite a lot of publicity about how XML has the advantage of being cross-platform and cross-language, unless it is applied to Web Services, developers often have problems with XML parsing in common Web applications, whether it is generated or processed on the server side or parsed in JavaScript by clients, which often results in complexityCode, very low development efficiency.In fact, for most Web applications, they don't need complex XML to transfer data at all, XML's scalability is rarely advantageous, and many AJAX applications even return HTML fragments directly to build dynamic Web pages.Returning HTML fragments greatly reduces system complexity, but also lacks flexibility, compared to returning XML and parsing it

 

 

 

 

Data format:

 

<span style="font-size:14px;">{
  "languages":[
    {"id":1,"ide":"Eclipse","name":"Java"},
    {"id":2,"ide":"XCode","name":"Swift"},
    {"id":3,"ide":"Visual","name":"C#"},
  ],
  "cat":"it"
}</span>

Read and create json code

 

 

package com.example.jsontest;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
	private  TextView  tv;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		tv=(TextView) findViewById(R.id.tv);
		tv.setMovementMethod(ScrollingMovementMethod.getInstance());
		//This is reading json data
/*		try{   
			InputStreamReader isr=new InputStreamReader(getAssets().open("test.json"),"utf-8");
			BufferedReader br=new BufferedReader(isr);
			String line;
		  StringBuilder builder=new StringBuilder();
			while((line=br.readLine())!=null){
				builder.append(line);
				System.out.println(line);
			}
			br.close();
			isr.close();		
			JSONObject root=new JSONObject(builder.toString());
			//System.out.println("1111111111"+root);
			System.out.println("Hello 11111111cat "+root.getString("cat");
			JSONArray arry=root.getJSONArray("languages");
			for(int i=0;i<arry.length();i++){
				JSONObject lan=arry.getJSONObject(i);
				System.out.println();
				tv.setText("id="+lan.getInt("id")+"name="+lan.getString("name")+
						"ide="+lan.getString("ide")+"\n");
				//tv.setText("name="+lan.getString("name"));
				//tv.setText("ide="+lan.getString("ide"));
				System.out.println("id="+lan.getInt("id"));
			    System.out.println("name="+lan.getString("name"));
				System.out.println("ide="+lan.getString("ide"));
			}
		}catch (Exception e) {
			// TODO: handle exception
			System.out.println("Exception ";
		}*/
	// This is creating json data		
		try {
			JSONObject root=new JSONObject();
			root.put("cat", "it");
			
			JSONObject lan1=new JSONObject();
			lan1.put("id", 1);  //First property
			lan1.put("ide", "Eclipse");
			lan1.put("name", "java");
			
			JSONObject lan2=new JSONObject();
			lan2.put("id", 2);   //Second attribute
			lan2.put("ide", "xcode");
			lan2.put("name", "Swift");
			
			JSONObject lan3=new JSONObject();
			lan3.put("id", 3);  //Third attribute
			lan3.put("ide", "Visual Studio");
			lan3.put("name", "C#");
			//Create an array
			JSONArray  array=new JSONArray();
			array.put(lan1);
			array.put(lan2);
			array.put(lan3);
			//Add Following Element
			root.put("languages", array);
			System.out.println(root.toString()); //Converts to a visible string.
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

 

 

 

 

 

 

259 original articles published, 2 praised and 6654 visited
Private letter follow

Keywords: JSON xml Android Javascript

Added by thipse_rahul on Sat, 08 Feb 2020 06:42:22 +0200