英文:
Parsing stringify string in JAVA
问题
s= s.replaceAll(""", "\"");
try {
s= s.replaceAll("\\n", "\\\\n");
JSONObject json = new JSONObject(s);
Log.d("SAVE_LOG", (String) json.get("version"));
} catch (JSONException e) {
Log.d("SAVE_LOG", String.valueOf(e));
e.printStackTrace();
}
import android.Manifest;
import android.annotation.SuppressLint;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentSender;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.provider.Settings;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.Toast;
import com.tcn.liftboard.control.PayMethod;
import com.tcn.liftboard.control.TcnVendEventID;
import com.tcn.liftboard.control.TcnVendEventResultID;
import com.tcn.liftboard.control.TcnVendIF;
import com.tcn.liftboard.control.VendEventInfo;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.List;
英文:
I do have string
{"version":"T2"}
I was reading it from webview and storing it in variable s. <br/>
I want to parse the value of version. So
try {
s= s.replaceAll("\n", "\\n");
JSONObject json = new JSONObject(s);
Log.d("SAVE_LOG", (String) json.get("version"));
} catch (JSONException e) {
Log.d("SAVE_LOG", String.valueOf(e));
e.printStackTrace();
}
I found this code from every site. <br/>
But it is giving me this error.
> org.json.JSONException: Value {"version":"T2"} of type
> java.lang.String cannot be converted to JSONObject
<br/>
My imports are
import android.Manifest;
import android.annotation.SuppressLint;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentSender;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.provider.Settings;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.Toast;
import com.tcn.liftboard.control.PayMethod;
import com.tcn.liftboard.control.TcnVendEventID;
import com.tcn.liftboard.control.TcnVendEventResultID;
import com.tcn.liftboard.control.TcnVendIF;
import com.tcn.liftboard.control.VendEventInfo;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.List;
答案1
得分: 2
这是我的代码示例:
import org.json.JSONException;
import org.json.JSONObject;
public class NewClass {
public static void main(String[] args) {
try {
String s = "{\"version\":\"T2\"}";
s = s.replaceAll("\n", "\\n");
JSONObject jsonObject = new JSONObject(s);
System.out.println(jsonObject.get("version"));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
输出:
T2
从Maven仓库下载JSON jar文件。
英文:
Here is my code
import org.json.JSONException;
import org.json.JSONObject;
public class NewClass {
public static void main(String[] args) {
try {
String s = "{\"version\":\"T2\"}";
s= s.replaceAll("\n", "\\n");
JSONObject jsonObject = new JSONObject(s);
System.out.println(jsonObject.get("version"));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
Output :
T2
Import json jar file from JSON jar download from maven
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论