Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从字符串读取值_Java_Android_String - Fatal编程技术网

Java 从字符串读取值

Java 从字符串读取值,java,android,string,Java,Android,String,我有一个字符串,其中存储了一些“名称”和“值”。 存储数据的样式如下所示: {"userId":6625,"OtherName":"blabla","isfirst":"true"} 我想知道如何获取字符串中“othername”的值, 或者我怎样才能得到“某个名字”的价值 我自己编写了一个代码: String str1; try { str1 = getStringFromFile(getApplicationContext().getFilesDir

我有一个字符串,其中存储了一些“名称”和“值”。 存储数据的样式如下所示:

{"userId":6625,"OtherName":"blabla","isfirst":"true"}
我想知道如何获取字符串中“othername”的值, 或者我怎样才能得到“某个名字”的价值

我自己编写了一个代码:

        String str1;
try {
            str1 = getStringFromFile(getApplicationContext().getFilesDir().getAbsolutePath()+"/myinfo.txt");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            str1 = "";
        }
        String str2;
        try{
        int startindeofuserid=str1.indexOf("userId");
        int lastindexofuserid = str1.indexOf(",", startindeofuserid);
        startindeofuserid = str1.indexOf(":", startindeofuserid);
        str2 = str1.substring(startindeofuserid, lastindexofuserid);
        str2 = str2.replaceAll(":", "");
        str2 = str2.replaceAll("\"", "");
        str2 = str2.replaceAll(" ", "");
        str2 = str2.replaceAll("'", "");
//For safety , cause my userid is just a long value.
        }catch(Exception e){
            str2 ="*"; 
        }
它工作得很好,但在一些设备中我得到了不同寻常的答案。 我只是想知道,下面的字符串,是否有某种数据存储方法?
是否有库可以将该字符串读入列表或数组?

您的存储格式类似于json,因此可以使用任何json解析器。例如:gson,jackson。

您的字符串是json格式的。
通过使用字符串创建
JSONObject
,可以从字符串中获取值,之后可以使用JSONObject中的
get
方法获取值。对于第一个

将字符串传递给
JSONObject
: 从
JSONObject
获取值:
检查此项以获取示例。

按“:”拆分字符串时,按“,”拆分字符串。 您将得到如下数组:

["Some Name", "6625"]
["OtherName", "blabla"]
["isfirst", "true"]

然后按类型解析值。

这将帮助您。。。将文本转换为字符串my_json。然后使用下面的代码

            if (my_json != null) {
                try {
                     jsonObj = new JSONObject(my_json);

                    if (jsonObj != null) {

                    int userId = jsonObj.getInt("userId");
                    String OtherName = jsonObj.getString("OtherName");
                           Log.e("OtherName",OtherName);



                    }

                } catch (JSONException e) {
                    e.printStackTrace();

                }

            } else {

                Log.e("JSON Data", "Didn't receive any data from server!");
            }

您可以使用Gson。将Gson jar添加到项目构建路径

        String  test ="{\"Some Name\":6625,\"OtherName\":\"blabla\",\"isfirst\":\"true\"}";

        Type type = new TypeToken<HashMap<String, String>>() {}.getType();
        System.out.println(new Gson().fromJson(test, type));        

        HashMap map =new Gson().fromJson(test, type);
        System.out.println(map.get("Some Name"));
        System.out.println(map.get("OtherName"));
        System.out.println(map.get("isfirst"));
String test=“{\'Some Name\':6625,\'OtherName\':\'blabla\',\'isfirst\':\'true\'”;
Type Type=new-TypeToken(){}.getType();
System.out.println(新的Gson().fromJson(test,type));
HashMap map=new Gson().fromJson(测试,类型);
System.out.println(map.get(“某个名称”);
System.out.println(map.get(“OtherName”);
System.out.println(map.get(“isfirst”);

首先,正如其他人鼓励的那样,一定要使用解析器,我自己使用Jackson

看看你的“数据”,有一点很奇怪:

"isfirst":"true"
如果您的目的是让解析器将其转换为布尔值,那么该值上就不会有引号。也就是说,上面是一个字符串,下面是一个布尔值:

"isfirst" : true
关于json的语法,以下内容可能会有所帮助:

使用json解析器从json字符串读取数据,因此它是一个json字符串?有很多json解析器可以处理这个问题。不要重新发明轮子。但是,如果您还必须处理非JSON的字符串,那么您可能必须继续使用自己的字符串。根据我的经验,JSON解析器对格式不正确的文本不是很宽容。这只是一个已经保存的txt文件,我不明白为什么要使用httpget和httppost!好的,我会给它一个goWorks的好,但我选择了@johny的答案供人们进一步使用(因为它很简单)。无论如何感谢您的帮助。@Mehrdad1373在将字符串转换为json对象时,您必须使用try-catch函数,您可以在他的答案中给出的链接中看到相同的内容。。。为了获得更好且无错误的结果,您必须使用if not null并尝试catch函数,否则应用程序有时会崩溃。非常感谢。
"isfirst":"true"
"isfirst" : true