So, say I have this JSON...
[
{
"a": "1",
"blah": "true"
},
{
"b": "2",
"blah": "false"
},
{
"c": "3",
"blah": "true"
}
]
...and then use jq to select certain entries...
jq '.[] | select(.blah=="true)'
I get this...
{
"a": "1",
"blah": "true"
}
{
"c": "3",
"blah": "true"
}
But I want it to look like...
[
{
"a": "1",
"blah": "true"
}
{
"c": "3",
"blah": "true"
}
]
...this, so that I can use indexing to get certain of these entries. How do I do that?
Simply indicate you want the result in a list by wrapping the expression with [];
> cat test.json | jq '[.[] | select(.blah=="true")]'
[
{
"a": "1",
"blah": "true"
},
{
"c": "3",
"blah": "true"
}
]
Using map to iterate over the array
jq 'map(select(.blah == "true"))'
I'm doing the Elm Guide exercise from Random Section and got stuck in step 5, where I have to flip around the dices before they settle on a final Value. I'm trying to do this calling the Cmd, that updates the view with the new values of the dices, 10 times with just one click of the button "Roll!" and put some kind of sleep function before each iteraction. For what I've read, Elm does not have a for, loop or while statement. The only way to loop is through recursion, but I'm not being able to adapt my code for it. My code below, does anyone have a suggestion?
import Browser
import Html exposing (..)
import Html.Events exposing (..)
import Svg exposing (..)
import Svg.Attributes as SvgAttr exposing (..)
import Random
-- MAIN
main =
Browser.element
{ init = init
, update = update
, subscriptions = subscriptions
, view = view
}
-- MODEL
type alias Model =
{ dieFace : Int
, dieFace2 : Int
}
init : () -> (Model, Cmd Msg)
init _ =
( Model 1 1
, Cmd.none
)
-- UPDATE
type Msg
= Roll
| NewFace (Int, Int)
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
Roll ->
( model
, Random.generate NewFace randomPair
)
NewFace (newFace, newFace2) ->
( Model newFace newFace2
, Cmd.none
)
weightedRoll : Random.Generator Int
weightedRoll =
Random.weighted
(1, 1)
[ (10, 2)
, (10, 3)
, (10, 4)
, (20, 5)
, (40, 6)
]
randomPair : Random.Generator (Int, Int)
randomPair =
Random.pair (Random.int 1 6) (Random.int 1 6)
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
-- VIEW
view : Model -> Html Msg
view model =
div []
[ h1 [] [ Html.text (String.fromInt model.dieFace) ]
, svg
[ width "120"
, height "120"
, viewBox "0 0 120 120"
]
(List.append
[ rect
[ x "10"
, y "10"
, width "100"
, height "100"
, rx "15"
, ry "15"
]
[]
]
(dicesSVG model.dieFace)
)
, div []
[ h1 [] [Html.text (String.fromInt model.dieFace2) ]
, svg
[ width "120"
, height "120"
, viewBox "0 0 120 120"
]
(List.append
[ rect
[ x "10"
, y "10"
, width "100"
, height "100"
, rx "15"
, ry "15"
]
[]
]
(dicesSVG model.dieFace2)
)]
, button [onClick Roll] [Html.text "Roll!"]
]
dicesSVG : Int -> List (Svg Msg)
dicesSVG number =
case number of
1 ->
[ circle [cx "60", cy "60", r "10", fill "white" ] [] ]
2 ->
[ circle [ cx "35", cy "35", r "10", fill "white" ] []
, circle [ cx "85", cy "85", r "10", fill "white" ] []
]
3 ->
[ circle [ cx "35", cy "35", r "10", fill "white" ] []
, circle [ cx "60", cy "60", r "10", fill "white" ] []
, circle [ cx "85", cy "85", r "10", fill "white" ] []
]
4 ->
[ circle [ cx "35", cy "35", r "10", fill "white" ] []
, circle [ cx "85", cy "35", r "10", fill "white" ] []
, circle [ cx "35", cy "85", r "10", fill "white" ] []
, circle [ cx "85", cy "85", r "10", fill "white" ] []
]
5 ->
[ circle [ cx "35", cy "35", r "10", fill "white" ] []
, circle [ cx "85", cy "35", r "10", fill "white" ] []
, circle [ cx "60", cy "60", r "10", fill "white" ] []
, circle [ cx "35", cy "85", r "10", fill "white" ] []
, circle [ cx "85", cy "85", r "10", fill "white" ] []
]
6 ->
[ circle [ cx "35", cy "35", r "10", fill "white" ] []
, circle [ cx "85", cy "35", r "10", fill "white" ] []
, circle [ cx "35", cy "60", r "10", fill "white" ] []
, circle [ cx "85", cy "60", r "10", fill "white" ] []
, circle [ cx "35", cy "85", r "10", fill "white" ] []
, circle [ cx "85", cy "85", r "10", fill "white" ] []
]
_ ->
[]
The trick is mostly in asking the runtime to send you messages.
The first thing you could try is changing
NewFace (newFace, newFace2) ->
( Model newFace newFace2
, Cmd.none
)
to
NewFace (newFace, newFace2) ->
( Model newFace newFace2
, Random.generate NewFace randomPair
)
This has the problem of never stopping, making your program spin around forever...
So you will need to track some stopping condition. Perhaps add a field to your model that tracks how many times it has rolled already? Then switching between Random.generate and Cmd.none based on that field.
Finally if you want a time delay between the rolls, then
import Task
wait : Float -> msg -> Cmd msg
wait delay msgToCallWhenDone =
Task.perform (always msg) (Process.sleep delay)
will give you a Cmd that will call whatever msg you give it after delay milliseconds. As a hint, you might want to introduce another msg for this one.
I'm trying to parse some JSON data with Json.Net. Here is my data:
[
{
"UIDClan": "1",
"UIDKnjiga": "1",
"Naslov": "Title1",
"DatumZaKada": "2013-08-09 00:00:00",
"DatumIstekRez": null,
"Spremno": "0"
},
{
"UIDClan": "1",
"UIDKnjiga": "2",
"Naslov": "Title2",
"DatumZaKada": "2013-08-08 00:00:00",
"DatumIstekRez": null,
"Spremno": "0"
},
{
"UIDClan": "1",
"UIDKnjiga": "3",
"Naslov": "Title3",
"DatumZaKada": "2013-08-09 00:00:00",
"DatumIstekRez": "2013-10-09 00:00:00",
"Spremno": "1"
}
]
With this piece of code i want to extract UIDClan data:
JObject o = JObject.Parse(s);
Console.WriteLine(o["UIDClan"]);
The error is
Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.
I've checked with JSONLint and it's valid.
The examples that I found doesn't start with [.
Am I doing something wrong?
You could try using a JArray.
This JSON data is actually an array.
JArray v = JArray.Parse(s);
To get the first item.
var firstItem = v[0]["UIDClan"].ToString();
You can even use linq
var items = v.Where(x => x["UIDClan"].ToString() == "1").ToList();
To overcome the error plz serialize the jsonstring in below format.this serialize string we are able parse as Jobject
Newtonsoft.Json.JsonConvert.SerializeObject(new {JsonString})
I created a sorter that StrCmpLogicalW / shlwapi.dll. unfortunately it causes errors on partial trust environments. I am in a really bad need of a solution that does not use 'shlwapi.dll' or StrCmpLogicalW and works in the same fashion.
Here's the sorter that causes the error.
Public Class nvSorter
Implements IComparer(Of String)
Declare Unicode Function StrCmpLogicalW Lib "shlwapi.dll" ( _
ByVal s1 As String, _
ByVal s2 As String) As Int32
Public Function Compare(ByVal x As String, ByVal y As String) As Integer
Implements System.Collections.Generic.IComparer(Of String).Compare
Return StrCmpLogicalW(x, y)
End Function
End Class
This should work:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Filenames() As String = New String() {"0", "1", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "2", "20", "3", "4", "5", "6", "7", "8", "9"}
Array.Sort(Filenames, New CustomComparer)
MessageBox.Show(String.Join(",", Filenames))
End Sub
End Class
Public Class CustomComparer
Implements IComparer(Of String)
Private Position As Integer
Private Order As Integer = 1
Public Sub New(Optional ByVal Ascending As Boolean = True)
If Not Ascending Then
Order = -1
End If
End Sub
Private Shared Function EmptyText(ByVal s As String) As Boolean
Return String.Empty.Equals(s)
End Function
Public Function Compare(ByVal x As String, ByVal y As String) As Integer Implements System.Collections.Generic.IComparer(Of String).Compare
Dim res1 As New List(Of String)(System.Text.RegularExpressions.Regex.Split(x, "(\d+)", System.Text.RegularExpressions.RegexOptions.IgnoreCase))
Dim res2 As New List(Of String)(System.Text.RegularExpressions.Regex.Split(y, "(\d+)", System.Text.RegularExpressions.RegexOptions.IgnoreCase))
res1.RemoveAll(AddressOf EmptyText)
res2.RemoveAll(AddressOf EmptyText)
Position = 0
For Each xstr As String In res1
If res2.Count > Position Then
If Not IsNumeric(xstr) AndAlso Not IsNumeric(res2(Position)) Then
Dim intresult As Integer = String.Compare(xstr, res2(Position), True)
If intresult <> 0 Then
Return intresult * Order
Else
Position += 1
End If
ElseIf IsNumeric(xstr) And Not IsNumeric(res2(Position)) Then
Return -1 * Order
ElseIf Not IsNumeric(xstr) And IsNumeric(res2(Position)) Then
Return 1 * Order
ElseIf IsNumeric(xstr) And IsNumeric(res2(Position)) Then
Dim res As Integer = Decimal.Compare(Decimal.Parse(xstr), Decimal.Parse(res2(Position)))
If res = 0 Then
Position += 1
Else
Return res * Order
End If
End If
Else
Return -1 * Order
End If
Next
Return 1 * Order
End Function
End Class
The result of the above sorting example of "0", "1", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "2", "20", "3", "4", "5", "6", "7", "8", "9" is:
I have a string array(strLarge) containing 5 values say 1,2,3,4,5.
I have another string(strSmall) containing a value say 3.
Now I need to remove this strSmall from strLarge and finally get the result as 1,2,4,5.
strLarge.Except(strSmall);
in LINQ
Produces the set difference of two
sequences.
See
msdn
string[] strLarge = { "1", "2", "3", "4", "5" };
string[] strSmall = { "3" };
strLarge = strLarge.Where(x => !strSmall.Contains(x)).ToArray();
use Except() for strLarge