如何从此JSON获取Name和UserName
String str =" {Name = jack,UserName = Jacki}"
在java android中
答案 0 :(得分:1)
试试这个,代码将使用scanner类及其findInLine()方法
String str="{Name=jack,UserName=Jacki}";
Scanner sc=new Scanner(str);
sc.findInLine("Name=");
if(sc.hasNext())
{
System.out.println(sc.next()); //This will print Name you can store it in variable as well
}
sc.findInLine("UserName=");
if(sc.hasNext())
{
System.out.println(sc.next()); //This will print UserName you can store it in variable as well
}
答案 1 :(得分:0)
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Sample
{
public static void main (String[] args) throws java.lang.Exception
{
String str="{Name=jack,UserName=Jacki}";
String[] str1=str.split(",");
System.out.print(str1[0].substring(1));
System.out.print(str1[1].substring(0,str1[1].length()-1));
}
}
所以在这个方法中你基本上把字符串分成“,”。这会给你{Name = jack和UserName = Jacki}。接下来的两个语句只解析这两个新字符串并删除“{”和“}”。