An warware: java shigo da jsonobject

shigo da jsonobject A cikin duniyar yau, aiki tare da bayanan JSON ya ฦ™ara zama mahimmanci, saboda ana amfani da shi sosai don watsa bayanai a cikin aikace-aikacen yanar gizo. ฦŠayan aiki gama gari lokacin aiki tare da bayanan JSON shine shigo da shi cikin shirin Java, musamman azaman JSONObject. A cikin wannan labarin, za mu bincika cikakkun bayanai game da shigo da bayanan JSON a matsayin JSONObject a cikin Java, samar da mafita ga matsalar, bayanin mataki-mataki na lambar, da kuma tattauna abubuwan da suka dace da ษ—akunan karatu da ayyuka da ke cikin tsari.

Mataki na farko na magance matsalar shine haษ—a da labura mai mahimmanci don aiki tare da bayanan JSON a Java. Don wannan, za mu iya amfani da mashahurin ษ—akin karatu na Java da ake kira JSON-java, wanda aka sani da ita org.json. Za mu fara ambata yadda ake ฦ™ara ษ—akin karatu zuwa aikinku ta hanyar Maven da Gradle, sannan za mu shiga cikin aiwatar da lambar.

<!-- Maven -->
<dependency>
  <groupId>org.json</groupId>
  <artifactId>json</artifactId>
  <version>20210307</version>
</dependency>

<!-- Gradle -->
implementation 'org.json:json:20210307'

Matakai na gaba sun haษ—a da aiwatar da lambar Java wanda ke karanta bayanan JSON kuma ya canza shi zuwa wani JSONObject. Don haka, za mu yi amfani da azuzuwan kamar FayilInputStream da kuma InputStreamReader don karanta abubuwan da ke cikin fayil ษ—in JSON sannan a rarraba su ta amfani da JSONTokener da kuma JSONObject azuzuwan.

Yin aiki tare da FileInputStream da InputStreamReader

Java yana ba da sabis FayilInputStream aji don karanta bayanan fayil a cikin bytes, wanda ke ba mu damar samun damar fayil ษ—in da ke ษ—auke da bayanan JSON. A hade, muna amfani da InputStreamReader aji don karanta rafin bayanan da canza shi zuwa haruffa don ba da damar sarrafawa.

import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class ImportJson {
  public static void main(String[] args) {
    // Prepare the FileInputStream
    try (FileInputStream fis = new FileInputStream("example.json");
         InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8)) {
      // Following code will read the JSON and convert it to JSONObject
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Yin nazarin bayanan JSON ta amfani da JSONTokener da JSONObject

Da zarar mun sami wani InputStreamReader abu don sarrafa abubuwan da ke cikin fayil ษ—in mu na JSON, za mu iya amfani da JSONTokener da kuma JSONObject azuzuwan don rarraba bayanan JSON zuwa wani abu Java mai amfani. The JSONTokener yana karanta jigon bayanan JSON ta hali daga InputStreamReader kuma ciyar da shi zuwa ga JSONObject magini don ฦ™irฦ™irar abu na ฦ™arshe.

import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import org.json.JSONObject;
import org.json.JSONTokener;

public class ImportJson {
  public static void main(String[] args) {
    // Prepare the FileInputStream and InputStreamReader
    try (FileInputStream fis = new FileInputStream("example.json");
         InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8)) {
      // Read JSON data using JSONTokener
      JSONTokener tokener = new JSONTokener(isr);

      // Create the JSONObject
      JSONObject jsonObject = new JSONObject(tokener);

      // Accessing data in the JSONObject
      String key = "example_key";
      if (jsonObject.has(key)) {
        System.out.println(jsonObject.get(key));
      }
      
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

A taฦ™aice, wannan labarin ya ba da cikakken bayani game da shigo da bayanan JSON da juyar da su zuwa bayanan da aka samu. JSONObject in Java. Ta hanyar amfani da JSON-java library, tare da azuzuwan Java kamar FayilInputStream, InputStreamReader, JSONTokener, Da kuma JSONObject, za ku iya rarrabawa da sarrafa bayanan JSON a cikin aikace-aikacen Java, inganta SEO da ฦ™warewar mai amfani yayin aiki tare da wannan sanannen tsarin bayanai.

Shafi posts:

Leave a Comment