在Java中解析字符串化的字符串。

huangapple go评论119阅读模式
英文:

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(&quot;\n&quot;, &quot;\\n&quot;);
        JSONObject json = new JSONObject(s);
        Log.d(&quot;SAVE_LOG&quot;, (String) json.get(&quot;version&quot;));
       } catch (JSONException e) {
         Log.d(&quot;SAVE_LOG&quot;, 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 = &quot;{\&quot;version\&quot;:\&quot;T2\&quot;}&quot;;
	         s= s.replaceAll(&quot;\n&quot;, &quot;\\n&quot;);
	         JSONObject jsonObject = new JSONObject(s);
	        
	        System.out.println(jsonObject.get(&quot;version&quot;));
	       } catch (JSONException e) {
	         e.printStackTrace();
	       }
	}

}

Output :

T2

Import json jar file from JSON jar download from maven

huangapple
  • 本文由 发表于 2020年4月4日 01:49:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/61017654.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定