AndroidでGSONを使用してHASHMAPをJSONに変換するにはどうすればよいですか?
GSONはJavaライブラリであり、OBJECTをJSONに、JSONをObjectに変換するために使用されます。内部的には、シリアル化と逆シリアル化に基づいて機能します。
この例は、GSONライブラリを使用してHASHAMPをJSONに変換する方法を示しています。
ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。
ステップ2 −build.gradleに次のコードを追加します。
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.andy.myapplication"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
} 上記のコードでは、GSON最新ライブラリを追加しました。
ステップ3 −次のコードをres / layout/activity_main.xmlに追加します。
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:app="https://schemas.android.com/apk/res-auto" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Result Data" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>
上記のコードでは、textviewを追加しました。このテキストビューには結果データが表示されます。
ステップ4 −次のコードをsrc / MainActivity.java
に追加しますimport android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView result=findViewById(R.id.result);
HashMap<String,String> hashMap=new HashMap<>();
hashMap.put("JAVA","NetBeans");
hashMap.put("Android","Android Studio");
hashMap.put("Kotlin", "Notepad ++");
Gson gson=new Gson();
String MapData=gson.toJson(hashMap);
result.setText(MapData);
}
} アプリケーションを実行してみましょう。実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 android studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[実行]アイコンをクリックします。
オプションとしてモバイルデバイスを選択し、デフォルトの画面を表示するモバイルデバイスを確認します-
上記の出力では、HASHAMPがJSONデータとして表示されています。
-
AndroidでJSON配列を繰り返す方法は?
この例は、AndroidでJSON配列を反復処理する方法を示しています。 ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。 ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://sche
-
AndroidでOCRを使用して画像をテキストに変換する方法
OCR(光学式文字認識)技術は、近年かなり進歩しています。画像からテキストをより正確にスキャンできるようになりました。モバイルデバイスで画像をテキストに変換できたらいいのではないでしょうか。これにより、画像をコンピュータにアップロードしてオンラインで変換する手間を省くことができます。この記事では、Androidデバイスを使用して画像をテキストに変換する方法を紹介します。 Androidで画像をテキストに変換する Playストアには多くのOCRベースのアプリがありますが、すべてが希望どおりに正確に機能するわけではありません。ただし、十分なテストを行った結果、正常に機能するアプリがいくつか見つか