110,535
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 将key是word的值保存到resList并返回
/// </summary>
/// <param name="input"></param>
/// <param name="key"></param>
/// <returns></returns>
private List<string> getStr(string input,string key)
{
List<string> resList = new List<string>();
int len = key.Length;
int start = 0;
while(start<input.Length-len)
{
int index1;
if((index1 = input.IndexOf(key,start))<=-1)
{
break;
}
int index2 = input.IndexOf(',', index1 + len);
resList.Add(input.Substring(index1 + len, index2 - index1 - len));
start = index2;
}
return resList;
}
//调用方法
string input= @"{
log_id: 1398984202156276557,
direction: 0,
words_result_num: 2,
words_result: [
{
words: 小明是小狗,
probability: {
variance: 0.0,
average: 0.999742,
min: 0.998732
}
},
{
words: 丽丽大帅哥,
probability: {
variance: 0.000435,
average: 0.987731,
min: 0.946409
}
}
],
language: -1
}";
List<string> result = getStr(input, "words:");
foreach(string s in result)
{
MessageBox.Show(s);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string ss = @"{
""log_id"": 1398984202156276557,
""direction"": 0,
""words_result_num"": 2,
""words_result"": [
{
""words"": ""小明是小狗"",
""probability"": {
""variance"": 0.0,
""average"": 0.999742,
""min"": 0.998732
}
},
{
""words"": ""丽丽大帅哥"",
""probability"": {
""variance"": 0.000435,
""average"": 0.987731,
""min"": 0.946409
}
}
],
""language"": -1
}";
MatchCollection matches = Regex.Matches(ss, "words\": \"[^\"]+");
foreach (Match match in matches)
{
Console.WriteLine(Regex.Match(match.Value, "[\u4e00-\u9fa5]+").Value);
}
}
}
}
static void Main(string[] args)
{
string json2 = @"{ 'MyArray': [ 'Manual text', '2000-05-23T00:00:00' ],'name':'HelenZhou' }";
JObject jsonObj = JObject.Parse(json2);
Console.WriteLine(jsonObj["name"].ToString());
Console.WriteLine(jsonObj.ToString());
}
foreach (var row in JObject.Parse(value)["words_result"])
{
Console.WriteLine(row["words"].ToString());
}